[英]iOs OpenGL ES 2.0 blending with shader on device and on simulator
[英]OpenGL ES: Screenshot shows different between device and simulator
我嘗試使用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,帶視網膜的iPhone和帶視網膜的iPad)模擬器(6.0版)。 但是,當我切換到真實設備(iPhone 4 / 4s / 5,iPad(2 / mini /視網膜))時,它僅顯示星狀圖像。 xcode版本是4.6.3,基本SDK是最新的IOS(IOS 6.1),並且IOS部署目標是5.0。 你們能告訴我如何解決嗎? 謝謝。
問題是IOS 6.0不會一直保留緩沖區,而是會擦除緩沖區。 但是,當您截屏時,您是從緩沖區中獲取數據的,因此這就是為什么我一直保持黑色背景。 因此添加類別以使設備保留緩沖區圖像將解決此問題。
@interface CAEAGLLayer (Retained)
@end
@implementation CAEAGLLayer (Retained)
- (NSDictionary*) drawableProperties
{
return @{kEAGLDrawablePropertyRetainedBacking : @(YES)};
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.