uiimage - 如何在不损失视网膜显示质量的情况下将UIView捕获到UIImage

标签 uiimage uikit scale retina-display image-capture

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

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

+ (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/

相关文章:

apache-flex - 缩放 Flex 对象导致旋转行为异常

ios - 我如何控制散点图的缩放/缩放以显示两个轴

ios - 如何使用六边形蒙版对 UIImage 进行圆角处理

ios - 来自 AVCaptureMetadataOutput 委托(delegate)的 UIImage (didOutputMetadataObjects)

ios - 获取 UIImageView 的位置

ios - 延迟一定时间后重复短促的声音

ios - 如何在单个 UIImage 上随机显示多个​​图像

ios - 是否可以使用 UITableViewStylePlain 禁用 UITableView 中的 float 标题?

html - 列在 Safari 中不可见 - UiKit v.2

android - 如何使用 ConstraintLayout 在 Android 中针对不同的屏幕尺寸缩放 UI 元素