猿问

如何在视网膜显示质量不变的情况下捕捉UIView到UI图像

如何在视网膜显示质量不变的情况下捕捉UIView到UI图像

我的代码适用于正常设备,但在视网膜设备上创建模糊图像。

有人知道解决我的问题吗?

+ (UIImage *) imageWithView:(UIView *)view{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;}


慕侠2389804
浏览 709回答 3
3回答

慕容森

不使用UIGraphicsBeginImageContext到UIGraphicsBeginImageContextWithOptions(如文件所示)在这页上)。为Scale传递0.0(第三个参数),您将得到一个与屏幕的缩放因子相等的上下文。UIGraphicsBeginImageContext使用的固定比例因子为1.0,因此在iPhone 4上的图像实际上与其他iPhone上的图像完全相同。我敢打赌,无论是iPhone 4在你隐式放大的时候应用了一个过滤器,还是你的大脑对它的感觉比它周围的一切都不那么清晰。所以,我猜:#import&nbsp;<QuartzCore/QuartzCore.h>+&nbsp;(UIImage&nbsp;*)imageWithView:(UIView&nbsp;*)view{ &nbsp;&nbsp;&nbsp;&nbsp;UIGraphicsBeginImageContextWithOptions(view.bounds.size,&nbsp;view.opaque,&nbsp;0.0); &nbsp;&nbsp;&nbsp;&nbsp;[view.layer&nbsp;renderInContext:UIGraphicsGetCurrentContext()]; &nbsp;&nbsp;&nbsp;&nbsp;UIImage&nbsp;*&nbsp;img&nbsp;=&nbsp;UIGraphicsGetImageFromCurrentImageContext(); &nbsp;&nbsp;&nbsp;&nbsp;UIGraphicsEndImageContext(); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;img;}在SWIFT 4中:func&nbsp;image(with&nbsp;view:&nbsp;UIView)&nbsp;->&nbsp;UIImage?&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;UIGraphicsBeginImageContextWithOptions(view.bounds.size,&nbsp;view.isOpaque,&nbsp;0.0) &nbsp;&nbsp;&nbsp;&nbsp;defer&nbsp;{&nbsp;UIGraphicsEndImageContext()&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;let&nbsp;context&nbsp;=&nbsp;UIGraphicsGetCurrentContext()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;view.layer.render(in:&nbsp;context) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;image&nbsp;=&nbsp;UIGraphicsGetImageFromCurrentImageContext() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;image&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;nil}

至尊宝的传说

我创建了一个基于@Dima解决方案的SWIFT扩展:extension&nbsp;UIImage&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;class&nbsp;func&nbsp;imageWithView(view:&nbsp;UIView)&nbsp;->&nbsp;UIImage&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UIGraphicsBeginImageContextWithOptions(view.bounds.size,&nbsp;view.opaque,&nbsp;0.0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;view.drawViewHierarchyInRect(view.bounds,&nbsp;afterScreenUpdates:&nbsp;true) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;img&nbsp;=&nbsp;UIGraphicsGetImageFromCurrentImageContext() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UIGraphicsEndImageContext() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;img&nbsp;&nbsp;&nbsp;&nbsp;}}编辑:SWIFT 4改进版extension&nbsp;UIImage&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;class&nbsp;func&nbsp;imageWithView(_&nbsp;view:&nbsp;UIView)&nbsp;->&nbsp;UIImage&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UIGraphicsBeginImageContextWithOptions(view.bounds.size,&nbsp;view.isOpaque,&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defer&nbsp;{&nbsp;UIGraphicsEndImageContext()&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;view.drawHierarchy(in:&nbsp;view.bounds,&nbsp;afterScreenUpdates:&nbsp;true) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;UIGraphicsGetImageFromCurrentImageContext()&nbsp;??&nbsp;UIImage() &nbsp;&nbsp;&nbsp;&nbsp;}}用法:let&nbsp;view&nbsp;=&nbsp;UIView(frame:&nbsp;CGRect(x:&nbsp;0,&nbsp;y:&nbsp;0,&nbsp;width:&nbsp;100,&nbsp;height:&nbsp;100))&nbsp;&nbsp;let&nbsp;image&nbsp;=&nbsp;UIImage.imageWithView(view)
随时随地看视频慕课网APP
我要回答