Ho cercato di convertire i dati RGBA di un array (int byte) in un UIImage. Il mio codice è simile come segue:Creazione di UIImmagine dai dati RGBA non elaborati
/*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;
L'immagine di output che ricevo è un'immagine di larghezza di 0, e l'altezza 1.080.950,784 mila (supponendo che la mia altezza e larghezza initil erano 240 e 240). Ho cercato di risolvere questo problema e ho controllato molti forum correlati, ad es. (link text) su come procedere, ma con scarso successo.
Si noti che 'kCGBitmapByteOrderDefault' non sembra trasferire il canale alfa sull'immagine. Usa 'kCGBitmapByteOrder32Big | kCGImageAlphaLast' invece. –
Penso che dovresti ripristinare colorSpaceRef ('CGColorSpaceRelease (colorSpaceRef)') prima di restituire newImage. – velkyel
@velkyel deve anche rilasciare il fornitore di dati 'CGDataProviderRelease (provider);' – medvedNick