はい、これは重複しているように聞こえます...しかし、多くの例と開発者ページでAppleの公式コードを試してみましたが、これはiPhone 4SおよびiPad 3で白い画像になります。iPad 1でうまく機能し、ただし、iPhone シミュレーター。
convertBitmapRGBA8ToUIImage に渡される幅と高さは、UIKit の「ポイント」ではなく、実際のピクセル単位です。つまり、iPad 1 では 1024, 768 になります。iPad 3 では 2048, 1536 です。
生データ バッファは、glReadPixels から読み取られ、tweetMessage() に渡す前に手動で反転された RGBA データです。
- (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];
}
}