是的,我知道这听起来像是重复的...但我已经在他们的开发者页面上尝试了很多例子,甚至Apple的官方代码,这导致了iPhone 4S上的白色图像。 iPad 3。 虽然在iPad 1和iPhone模拟器上运行良好。
传递给convertBitmapRGBA8ToUIImage的宽度和高度是实际像素,而不是UIKit'points'。即,在iPad 1上,它将是1024,768。在iPad 3上,它是2048,1536。
原始数据缓冲区是从glReadPixels读取的RGBA数据,并在传递给tweetMessage()之前手动翻转。
- (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];
}
}