我可以使用以下代码从iPhone 6的UIView
截屏:
在这里,我已经打印了2条日志。首先给我图像尺寸{375, 667}
,但将其转换为PNG格式后,给我图像尺寸{750, 1334}
。
我知道它正在将其转换为视网膜显示。每个点在屏幕上由2个像素表示,因此保存的图像是分辨率的两倍。
但是我需要PNG格式的图像尺寸{375, 667}
。
- (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;
}
大图:
小图片:-
最佳答案
只是为了不至于质量太差,您可以创建具有屏幕比例的图像
但是当您加载时可以使用此
CGFloat screenScale = [UIScreen mainScreen].scale;
if (screenScale != img.scale) {
img = [UIImage imageWithCGImage:img.CGImage scale:screenScale orientation:img.imageOrientation];
}