【问题标题】:Screenshot Using UIGraphicsBeginImageContextWithOptions For iPad 3 (Retina)使用适用于 iPad 3 (Retina) 的 UIGraphicsBeginImageContextWithOptions 的屏幕截图
【发布时间】:2012-05-22 13:55:32
【问题描述】:

我正在使用以下代码截屏:

    // 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 来测试它)。

还有什么我做错了吗?

【问题讨论】:

    标签: ios retina-display ipad-3


    【解决方案1】:

    所以我终于拿到了一个物理 iPad Retina,并且我最初发布的代码运行良好。生成的图像确实具有完整的 Retina 分辨率。

    【讨论】:

      【解决方案2】:

      这是我正在使用的代码,我似乎对 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];
      

      【讨论】:

        猜你喜欢
        • 2016-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-07
        • 1970-01-01
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多