Swift:UIImageView 的 CropView

Swift: CropView for UIImageView

我正在寻找一个教程,告诉我如何创建一个框架来裁剪照片,如下图

谢谢

作为第一步,请考虑实施使用 corners/handles 调整大小的 UIView。这 post 应该有帮助

一旦我们有了可调整大小的 UIView,然后使用 UIView.frame 的原点和大小来裁剪图像,如下所示:

extension UIImage {

    func crop(size: CGSize, offset: CGPoint, scale: CGFloat = 1.0) -> UIImage? {
        let rect = CGRect(x: offset.x * scale, y: offset.y * scale, width: size.width * scale, height: size.height * scale)
        if let cropped = self.cgImage?.cropping(to: rect) {
            return UIImage(cgImage: cropped)
        }
        return nil
    }

}

let croppedImage = image.crop(size: resizableView.frame.size, offset: resizableView.frame.origin)