Screenshot Using UIGraphicsBeginImageContextWithOptions for iPad 3 (Retina)

I am taking a screenshot using the following code:

// Returns 1024x768 for iPad Retina CGSize screenDimensions = [[UIScreen mainScreen] bounds].size; // Create a graphics context with the target size // (last parameter takes scale into account) UIGraphicsBeginImageContextWithOptions(screenDimensions, NO, 0); // Render the view to a new context CGContextRef context = UIGraphicsGetCurrentContext(); [myView.layer renderInContext:context]; // Save to Camera Roll UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIImageWriteToSavedPhotosAlbum(screenshot, self, nil, nil); UIGraphicsEndImageContext(); 

This works, however, I have a report from the user that this results in an image in the Camera Roll that does not match the iPad's retina resolution. Rather, it is more like iPad resolution without a retina. (I don't have an iPad 3 to check this out).

Is there anything else I'm doing wrong?

+4
source share
2 answers

So, I finally got a physical iPad Retina, and the code I posted initially works fine. The resulting image seems to be in full resolution Retina.

+2
source

Here is the code I'm using and I have no problems with the iPad 3. You can also confirm the use of the iPad retina in iOS Simulator. My code also saves the file in the document directory. You can change it as you see fit.

  CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast); CGContextTranslateCTM(ctx, 0.0, screenSize.height); CGContextScaleCTM(ctx, 1.0, -1.0); [(CALayer*)self.view.layer renderInContext:ctx]; CGImageRef cgImage = CGBitmapContextCreateImage(ctx); UIImage *image = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); CGContextRelease(ctx); CGColorSpaceRelease(colorSpaceRef); NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@/myscreenshot.jpg", docDir]; [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES]; 
0
source

Source: https://habr.com/ru/post/1413756/


All Articles