Cropping an UIImage

后端 未结 24 1834
轮回少年
轮回少年 2020-11-22 02:45

I\'ve got some code that resizes an image so I can get a scaled chunk of the center of the image - I use this to take a UIImage and return a small, square repre

24条回答
  •  故里飘歌
    2020-11-22 03:36

    Here's an updated Swift 3 version based on Noodles answer

    func cropping(to rect: CGRect) -> UIImage? {
    
        if let cgCrop = cgImage?.cropping(to: rect) {
            return UIImage(cgImage: cgCrop)
        }
        else if let ciCrop = ciImage?.cropping(to: rect) {
            return UIImage(ciImage: ciCrop)
        }
    
        return nil
    }
    

提交回复
热议问题