UIActivityViewController を使用してスクリーンショットを作成し、iPhone/iPad デバイスの写真に保存しようとしました。ただし、シミュレーターではすべてが正しく表示されますが、デバイスに切り替えると一部しか表示されません。スクリーンショットは次のとおりです。
- シミュレーター (背景が 1 つ、緑の線が 1 つ、星のイメージが 1 つあることがわかります)
- 実際のデバイスでは (星の画像が 1 つだけで、それ以外はすべてなくなっていることがわかります)
スクリーンショットを撮れるように、これら 3 つの異なる UIImage をすべて 1 つの画像にマージしました。
まず、背景画像 (ブリッジ UIImage) を星の画像とマージします。
-(UIImage*)mergeUIImageView:(UIImage*)bkgound FrontPic:(UIImage*)fnt FrontPicX:(CGFloat)xPos FrontPicY:(CGFloat)yPos FrontPicWidth:(CGFloat)picWidth FrontPicHeight:(CGFloat)picHeight FinalSize:(CGSize)finalSize { UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)); // bkgound - is the bridge image [bkgound drawInRect:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)]; // fnt - is the star image [fnt drawInRect:CGRectMake(xPos, yPos, picWidth, picHeight)]; // merged image UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
次に、この画像を緑の線であるopenglでレンダリングされた画像とマージしました。
a) まず、この関数を使用して、opengGL イメージを UIImage に変更します。
-(UIImage *) glToUIImage {
float scaleFactor = [[UIScreen mainScreen] scale];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat image_height = screen.size.width * scaleFactor;
CGFloat image_width = screen.size.height * scaleFactor;
NSInteger myDataLength = image_width * image_height * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, image_width, image_height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < image_height; y++)
{
for(int x = 0; x < image_width * 4; x++)
{
buffer2[(int)((image_height - 1 - y) * image_width * 4 + x)] = buffer[(int)(y * 4 * image_width + x)];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * image_width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(image_width, image_height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}
b)次に、上記の同じ関数を使用して、この opengl 画像を上記の画像 (ブリッジ + スター) とマージします。
-(UIImage*)screenshot
{
// get opengl image from above function
UIImage *image = [self glToUIImage];
CGRect pos = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(image.size);
[image drawInRect:pos];
[self.background.image drawInRect:pos];
UIImage* final = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return final;
}
また、(iPhone、iPad、Retina 搭載の iPhone、Retina 搭載の iPad) シミュレーター (バージョン 6.0) でうまく動作します。しかし、実際のデバイス (iPhone 4/4s/5、iPad (2/mini/retina)) に切り替えると、星の画像しか表示されません。xcode のバージョンは 4.6.3 で、ベース SDK は最新の IOS (IOS 6.1) であり、IOS の展開ターゲットは 5.0 です。修正方法を教えていただけますか?ありがとう。