【发布时间】:2013-09-16 15:14:47
【问题描述】:
我尝试使用 UIActivityViewController 创建屏幕截图并将其保存到 iPhone/iPad 设备中的照片中。但是,在模拟器中,一切都正确显示,但是当我切换到设备时,它只显示了一部分。这是截图:
- 在模拟器中(可以看到一背景一绿线一星图)
- 在真机中(可以看到只有一个星图,其他都没有了)
我将所有这三个不同的 UIImage 合并为一张图像,以便我可以截屏。
-
我首先将背景图片(桥 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。你们能告诉我如何解决它吗?谢谢。
【问题讨论】:
-
是的,一切都在那里,我还检查了加载图像的拼写。我将名称更改为与我的资源图片完全相同(大写和小写的东西)。
标签: ios objective-c opengl-es opengl-es-2.0 screenshot