繁体   English   中英

OpenGL ES:视网膜和非视网膜设备的屏幕截图大小不同

[英]OpenGL ES: Screenshot size is different in retina and non-retina devices

我是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图像仅显示了视网膜设备中的一部分,如何解决此问题。 谢谢 !!!

您的代码假定视图大小等于像素,但不是。 是要点。 您需要转换为每个设备的实际像素大小。 UIScreen为此具有scale属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM