2010-12-28 17 views
13

私は配列RGBAデータ(intバイト)をUIImageに変換しようとしています。次のように私のコードは次のようになります。生のRGBAデータからUIImageを作成

/*height and width are integers denoting the dimensions of the image*/ 
unsigned char *rawData = malloc(width*height*4); 

for (int i=0; i<width*height; ++i) 
{ 
    rawData[4*i] = <red_val>; 
    rawData[4*i+1] = <green_val>; 
    rawData[4*i+2] = <blue_val>; 
    rawData[4*i+3] = 255; 
} 


/*I Have the correct values displayed 
    - ensuring the rawData is well populated*/ 

NSLog(@"(%i,%i,%i,%f)",rawData[0],rawData[1],rawData[2],rawData[3]/255.0f); 
NSLog(@"(%i,%i,%i,%f)",rawData[4],rawData[5],rawData[6],rawData[7]/255.0f); 
NSLog(@"(%i,%i,%i,%f)",rawData[8],rawData[9],rawData[10],rawData[11]/255.0f); 



CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, 
                  rawData, 
                  width*height*4, 
                  NULL); 

int bitsPerComponent = 8; 
int bitsPerPixel = 32; 
int bytesPerRow = 4*width; 
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 
CGImageRef imageRef = CGImageCreate(width, 
            height, 
            8, 
            32, 
            4*width,colorSpaceRef, 
            bitmapInfo, 
            provider,NULL,NO,renderingIntent); 
/*I get the current dimensions displayed here */ 
NSLog(@"width=%i, height: %i", CGImageGetWidth(imageRef), 
     CGImageGetHeight(imageRef)); 
UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 
/*This is where the problem lies. 
    The width, height displayed are of completely different dimensions 
    viz. the width is always zero and the height is a very huge number */ 

NSLog(@"resultImg width:%i, height:%i", 
      newImage.size.width,newImage.size.height); 


return newImage; 

私が受け取った出力画像は、幅0の画像であり、高さは1080950784(私のinitil高さと幅は240と240だったと仮定した場合)。私はこれを選別して、多くの関連フォーラムをチェックしました。 (link text)、成功への道はほとんどありません。

+1

「kCGBitmapByteOrderDefault」はアルファチャンネルを画像に転送していないようです。代わりに 'kCGBitmapByteOrder32Big | kCGImageAlphaLast'を使用してください。 –

+0

newImageを返す前にcolorSpaceRef( 'CGColorSpaceRelease(colorSpaceRef)')を再配置するべきだと思います。 – velkyel

+0

@velkyel彼はまた、データプロバイダ 'CGDataProviderRelease(provider);を解放しなければなりません。 – medvedNick

答えて

9

私たちの両者が見落とした、かなりばかげたミスであることが判明しました。 UIImageの寸法は、整数ではなく浮動小数点数として格納されます。 :D

は、代わりに

NSLog(@"resultImg width:%f, height:%f", 
      newImage.size.width,newImage.size.height); 

を試してみてください。イメージサイズが正しく転送されました。

2

問題は解決しました。私は表示したい画像を取得しますが、幅と高さが異なる理由はまだ分かりません。本質的に、プログラムごとに間違ったことは何も言いません。唯一の問題は幅と高さです。

2

私はこの問題を単独で確認しただけで、Appleにバグとして報告する必要があります。 +(UIImage)imageWithCGImage:は、ソースCGImageRefの幅と高さをUIImageに正しく転送しません。

関連する問題