2010-12-28 17 views
13

Próbowałem przekonwertować dane RGBA z tablicy (int bajtów) na obiekt UII. Moje kod wygląda następująco:Tworzenie obiektu UII z surowych danych RGBA

/*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; 

Wyjście obrazu, które otrzymuję jest obraz o szerokości 0, a wysokość 1080950784 (zakładając mój initil wysokość i szerokość były 240 i 240). Próbowałem rozwiązać ten problem i sprawdziłem wiele powiązanych forów, np. (link text) o tym, jak to zrobić, ale z niewielkim sukcesem.

+1

Należy zauważyć, że 'kCGBitmapByteOrderDefault' nie przenosi kanału alfa do obrazu. Zamiast tego użyj 'kCGBitmapByteOrder32Big | kCGImageAlphaLast'. –

+0

Myślę, że powinieneś zwolnić colorSpaceRef ('CGColorSpaceRelease (colorSpaceRef)') przed zwróceniem newImage. – velkyel

+0

@velkyel on również musi zwolnić dostawcę danych 'CGDataProviderRelease (dostawca);' – medvedNick

Odpowiedz

9

Okazuje się, że problemem jest całkiem głupi błąd, który przeoczyliśmy oboje. Wymiary UIImage są przechowywane jako zmiennoprzecinkowe, a nie całkowite. : D

Spróbuj

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

Zamiast. Rozmiar obrazu został poprawnie przesłany.

2

Problem został rozwiązany. Otrzymuję obraz, który chcę wyświetlać, ale wciąż nie mogę zrozumieć, dlaczego szerokość i wysokość są różne. Zasadniczo nic złego w programie na głos. Jedynym problemem jest szerokość i wysokość.

2

Po prostu niezależnie zweryfikowałem ten problem, myślę, że powinien on zostać zgłoszony jako błąd dla Apple. +(UIImage)imageWithCGImage: nie przesyła poprawnie szerokości i wysokości źródła CGImageRef do UIImage.

Powiązane problemy