【问题标题】:IOS Swift UIImage crop to given rect sizeIOS Swift UIImage裁剪到给定的矩形大小
【发布时间】:2018-05-14 01:40:29
【问题描述】:

我尝试将 UIImage 裁剪为给定的矩形,但它没有裁剪常规大小 矩形大小来自scrollView

这里是参考代码:

guard let view = imageCrop else { print("Error"); return }

        let normalizedX = view.contentOffset.x / view.contentSize.width
        let normalizedY = view.contentOffset.y / view.contentSize.height

        let normalizedWidth  = view.frame.width / view.contentSize.width
        let normalizedHeight = view.frame.height / view.contentSize.height

        let cropRect = CGRect(x: normalizedX, y: normalizedY,
                              width: normalizedWidth, height: normalizedHeight)

cropImage(image: imageCrop.image, cropRect: cropRect)



func cropImage(imageToCrop:UIImage, toRect rect:CGRect) -> UIImage{

        let imageRef:CGImage = imageToCrop.cgImage!.cropping(to: rect)!
        let cropped:UIImage = UIImage(cgImage:imageRef)
        return cropped
    }

【问题讨论】:

    标签: ios swift uiimage crop


    【解决方案1】:

    解决了。

     var cropArea:CGRect{
            get{
                let factor = imageCrop.image!.size.width/view.frame.width
                let scale = 1/imageCrop.zoomScale
                let imageFrame = imageCrop.frame
                let x = (imageCrop.contentOffset.x + imageCrop.frame.origin.x - imageFrame.origin.x) * scale * factor
                let y = (imageCrop.contentOffset.y + imageCrop.frame.origin.y - imageFrame.origin.y) * scale * factor
                let width = imageCrop.frame.size.width * scale * factor
                let height = imageCrop.frame.size.height * scale * factor
                return CGRect(x: x, y: y, width: width, height: height)
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-05
      • 2017-05-09
      • 2012-02-11
      • 2012-01-03
      • 2010-10-10
      • 1970-01-01
      相关资源
      最近更新 更多