使用
let image = xxx
let retImage = image.clipedImageInRect(rect)
封装
extension UIImage {
func clipedImageInRect(_ targetRect:CGRect) -> UIImage {
let scale = UIScreen.main.scale
let realRect = CGRect(x: targetRect.origin.x * scale, y: targetRect.origin.y * scale, width: targetRect.width * scale, height: targetRect.height * scale)
guard let cgImage = self.cgImage?.cropping(to: realRect) else {
return UIImage()
}
let ret = UIImage(cgImage: cgImage, scale: scale, orientation: .up)
return ret
}
}
网友评论