Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何根据屏幕比例裁剪图像_Ios_Iphone_Swift_Image_Uiimage - Fatal编程技术网

Ios 如何根据屏幕比例裁剪图像

Ios 如何根据屏幕比例裁剪图像,ios,iphone,swift,image,uiimage,Ios,Iphone,Swift,Image,Uiimage,我正在尝试使用裁剪框UIView裁剪UIImage,用户可以在图像视图中的任意位置拖动该裁剪框进行裁剪。我用来计算裁剪矩形的逻辑如下: extension UIImageView { public func computeCropRect(for sourceFrame : CGRect) -> CGRect { let widthScale = bounds.size.width / image!.size.width let heightS

我正在尝试使用裁剪框
UIView
裁剪
UIImage
,用户可以在图像视图中的任意位置拖动该裁剪框进行裁剪。我用来计算裁剪矩形的逻辑如下:

 extension UIImageView {

    public func computeCropRect(for sourceFrame : CGRect) -> CGRect {

        let widthScale = bounds.size.width / image!.size.width
        let heightScale = bounds.size.height / image!.size.height

        var x : CGFloat = 0
        var y : CGFloat = 0
        var width : CGFloat = 0
        var height : CGFloat = 0
        var offSet : CGFloat = 0

        if widthScale < heightScale {
            offSet = (bounds.size.height - (image!.size.height * widthScale))/2
            x = sourceFrame.origin.x / widthScale
            y = (sourceFrame.origin.y - offSet) / widthScale
            width = sourceFrame.size.width / widthScale
            height = sourceFrame.size.height / widthScale
        } else {
            offSet = (bounds.size.width - (image!.size.width * heightScale))/2
            x = (sourceFrame.origin.x - offSet) / heightScale
            y = sourceFrame.origin.y / heightScale
            width = sourceFrame.size.width / heightScale
            height = sourceFrame.size.height / heightScale
        }

        return CGRect(x: x, y: y, width: width, height: height)
    }
}
如果我改为使用:

UIGraphicsBeginImageContext(frame.size)

我的裁剪代码将是准确的,但绘图保真度将看起来颗粒状和低质量,因为我没有考虑视网膜屏幕设备。我的问题是如何修改裁剪函数以考虑
UIScreen.main.scale

您有解决方案吗?您有解决方案吗?
UIGraphicsBeginImageContextWithOptions(frame.size, false, UIScreen.main.scale)
UIGraphicsBeginImageContext(frame.size)