是的,我知道这听起来像是重复的......但我在他们的开发者页面上尝试了很多示例,甚至是 Apple 的官方代码,这导致 iPhone 4S 和 iPad 3 上出现白色图像。在 iPad 1 和iPhone模拟器虽然。
传递给 convertBitmapRGBA8ToUIImage 的宽度和高度是实际像素,而不是 UIKit 的“点”。即,在 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];
}
}