我的代码适用于普通设备,但在视网膜设备上创建模糊的图像。
有人知道我的问题的解决方案吗?
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
最佳答案
从使用UIGraphicsBeginImageContext
切换为UIGraphicsBeginImageContextWithOptions
(如on this page所述)。传递0.0作为比例(第三个参数),您将获得一个比例因子等于屏幕比例的上下文。UIGraphicsBeginImageContext
使用1.0的固定比例因子,因此您实际上在iPhone 4上获得与其他iPhone完全相同的图像。我敢打赌,当您隐式放大iPhone 4时,它会应用一个滤镜,或者只是您的大脑正在察觉它不如它周围的一切锐利。
所以,我猜:
#import <QuartzCore/QuartzCore.h>
+ (UIImage *)imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
在Swift 4中:
func image(with view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
defer { UIGraphicsEndImageContext() }
if let context = UIGraphicsGetCurrentContext() {
view.layer.render(in: context)
let image = UIGraphicsGetImageFromCurrentImageContext()
return image
}
return nil
}
关于uiimage - 如何在不损失视网膜显示质量的情况下将UIView捕获到UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16349301/