0

我可以UIView使用以下代码从 iPhone 6 截取屏幕截图:-

在这里,我打印了 2 个日志。首先给我图像大小{375, 667},但在将其转换为 PNG 格式后,它给了我图像大小{750, 1334}

我知道它正在将其转换为视网膜显示。每个点在屏幕上由 2 个像素表示,因此保存的图像是分辨率的两倍。

但我需要{375, 667}PNG格式的图像大小。

- (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSLog(@"Image Size: %@", image);  // Image Size: <UIImage: 0x1700880c0>, {375, 667}

    NSData* imageData =  UIImagePNGRepresentation(image);
    UIImage* pngImage = [UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil);

    NSLog(@"PNG Image Size: %@", pngImage);  // PNG Image Size: <UIImage: 0x174087760>, {750, 1334}

    return image;
}


大图:- 在此处输入图像描述

小图:-

在此处输入图像描述

4

2 回答 2

1

只是为了不降低质量,您可以创建具有屏幕比例的图像

但是当你加载时你可以使用它

CGFloat screenScale = [UIScreen mainScreen].scale;
if (screenScale != img.scale) {
    img = [UIImage imageWithCGImage:img.CGImage scale:screenScale    orientation:img.imageOrientation];
}
于 2017-08-21T05:18:44.273 回答
0

将比例更改为 1 而不是[UIScreen mainScreen].scale

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1);
于 2017-08-21T04:50:49.907 回答