UIGraphicsBeginImageContextWithOptions 和 UIImageJPEGRepresentation 不能很好地协同工作
所以我有这段代码来创建一个 UIImage:
UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0);
[border.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
此时,图像的尺寸是正确的,80x100。
然后它运行以下代码:
NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f);
图像的 NSData 返回尺寸为 160x200 的图像 - 是应有尺寸的两倍。
很明显,原因是这一行:
UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0);
末尾的 0 是比例,因为它是 0,所以它按照设备比例因素。我保持这种方式是为了保持清晰的图像。然而,当我将图像设置为 1 时,尽管图像保持应有的大小,但它并没有以视网膜质量显示。我想做的是保持它的视网膜质量,但也保持它在正确的尺寸。有办法做到这一点吗?
So i have this code to create a UIImage:
UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0);
[border.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
At this point, the size on the image is correct, 80x100.
Then it runs this code:
NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f);
And the NSData of the image returns an image at the size 160x200 - twice as much as it should be.
It's became clear the reason for this is the line:
UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0);
The 0 on the end is the scale, and because it's 0 it goes by the devices scale factor. I keep it this way to maintain a clear image. However, when i set the image to 1, despite the image staying the size it should, it doesn't come out in retina quality. What i want to do is keep it in retina quality, but also keep it at the right size. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用 UIImageJPEGRepresentation 之前尝试调整 UIImage 的大小
Try resizing the UIImage before calling UIImageJPEGRepresentation