【问题标题】:Crop Image so It Becomes Full Screen like Snapchat裁剪图像,使其像 Snapchat 一样变成全屏
【发布时间】:2019-12-13 17:33:25
【问题描述】:

如果您将图片上传到 Snapchat(尚未全屏),它将放大并裁剪照片,使其变为全屏。我可以在我的 ImageView 中使用自动调整大小的蒙版来做到这一点,但我需要能够将图像保存在这种裁剪状态,我不知道该怎么做。

这就是我能够在图像视图中以我想要的方式显示图像(从相机胶卷中选择)的方式

let imgView = UIImageView(image: image)

        imgView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        imgView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

        self.view.addSubview(imgView)

这会转换非全屏照片并使用支撑缩放/裁剪全屏显示。现在如何将照片保存为全屏照片?

【问题讨论】:

    标签: swift image uiimageview uiimage


    【解决方案1】:

    您可以从给定的视图中捕获图像。

    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
    }
    
    let img = image(with: YourImageView)
    

    【讨论】:

    • 这是否会大大降低图像的质量,因为它从 ~3000x4000 变为喜欢 ~414x730?还是我没有正确考虑这一点
    • 是的,图像的尺寸将适合您的视图。
    • 即使分辨率从 ~3000x4000 下降到 ~400x700,为什么图像的质量看起来与原始图像相同? iPhone 从来没有显示 ~3000x4000,因为屏幕只有 ~400x700?
    • 是的,Here 都是 iPhone 分辨率。顺便说一句,pt 是 iOS 中的分辨率单位,表示不同设备的 2 或 3 倍像素。
    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2013-01-14
    • 2012-05-15
    相关资源
    最近更新 更多