Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert raw RGBA to UIImage
    text
    copied!<p>Yes, I know this sounds like a duplicate... but I've tried lots of examples and even Apple's official code on their developer page and this is resulting in a white image on iPhone 4S &amp; iPad 3. Works great on iPad 1 and the iPhone Simulator though.</p> <p>width and height passed to convertBitmapRGBA8ToUIImage is in actual pixels, not UIKit 'points'. I.e., on iPad 1, it would be 1024, 768. On iPad 3, it's 2048, 1536.</p> <p>The raw data buffer is RGBA data read from glReadPixels and flipped manually before passing to tweetMessage().</p> <pre><code>- (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer withWidth:(int) width withHeight:(int) height { CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, buffer, width * height * 4, NULL); CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast, ref, NULL, true, kCGRenderingIntentDefault); float scale = 1.0f; if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { scale = [[UIScreen mainScreen] scale]; } UIGraphicsBeginImageContextWithOptions(CGSizeMake(width/scale, height/scale), NO, scale); CGContextRef cgcontext = UIGraphicsGetCurrentContext(); CGContextSetBlendMode(cgcontext, kCGBlendModeCopy); // Image needs to be flipped BACK for CG CGContextTranslateCTM(cgcontext, 0, height/scale); CGContextScaleCTM(cgcontext, 1, -1); CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, width/scale, height/scale), iref); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CFRelease(ref); CFRelease(colorspace); CGImageRelease(iref); return image; } - (void)tweetMessage:(const char *)message withURL:(const char *)url withImage:(unsigned char*)rawRGBAImage withWidth:(unsigned int)imageWidth withHeight:(unsigned int)imageHeight { UIImage *tweetImage = nil; if (rawRGBAImage != nil) { // Convert raw data to UIImage tweetImage = [self convertBitmapRGBA8ToUIImage:rawRGBAImage withWidth:imageWidth withHeight:imageHeight]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload