使用UIGraphicsBeginImageContextWithOptions截屏iPad 3 (Retina)

4
我正在使用以下代码进行截屏:
    // 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();

这样做是可以的,但是有用户报告说在相机胶卷中生成的图像不是 iPad 视网膜分辨率,而更像 iPad 非视网膜分辨率。(我没有 iPad 3 来测试这个问题。)

还有其他我做错了什么吗?

2个回答

2

最近我终于拿到了一台iPad Retina,我最初发布的代码在这台设备上也可以正常使用。生成的图片看起来确实是以完整的Retina分辨率显示的。


0

这是我正在使用的代码,我似乎在iPad 3上没有任何问题。您还可以在iOS模拟器中使用视网膜iPad进行确认。我的代码还将文件保存到文档目录中。您可以根据需要进行修改。

    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];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接