2010-12-28 3 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; 

I 0 폭 및 높이 1,080,950,784의 이미지 수신 출력 이미지 (가정 내 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으로 올바르게 전송하지 않습니다.

관련 문제