gpt4 book ai didi

ios - OpenGL ES : Screenshot size is different in retina and non-retina devices

转载 作者:行者123 更新时间:2023-11-29 03:39:06 24 4
gpt4 key购买 nike

我是 opengl es 2.0 开发的新手。我从屏幕截图中获得的 UIImage 在非视网膜设备(iphone 4 和 ipad)上看起来不错,但是当我从视网膜设备上获得屏幕截图时,它似乎放大了。这是我使用的代码。

-(UIImage *) glToUIImage {

CGSize size = self.view.frame.size;

// the reason I set the height and width up-side-down is because my
// screenshot captured in landscape mode.
int image_height = (int)size.width;
int image_width = (int)size.height;

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[(image_height - 1 - y) * image_width * 4 + x] = buffer[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;
}

// screenshot function, combined my opengl image with background image and
// saved into Photos.
-(UIImage*)screenshot
{
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();

// final picture I saved into Photos.
return final;
}

功能正常,但是opengl图片在retina设备上只显示部分,请问如何解决。谢谢!!!

最佳答案

您的代码假设 View 大小等于像素,但事实并非如此。是积分。您需要转换为每个设备的实际像素大小。 UIScreen 有一个 scale 属性。

关于ios - OpenGL ES : Screenshot size is different in retina and non-retina devices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18707496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com