I want to implement a view controller that can crop an image using the frame of the red CGRect
. I tried the following code but it seems like my cropping logic is way off. The cropped image seems to be a tiny portion of the original image... How can I grab an UIImage of just whatever inside my red rectangle CGRect
?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil)
if let x = info[UIImagePickerControllerOriginalImage] as? UIImage {
let a = x.cgImage?.cropping(to: CGRect(x: 100, y: 100, width: 200, height: 50))
let b = UIImage(cgImage: a!)
let y = UIImageView(frame: self.view.frame)
y.image = b
self.view.addSubview(y)
}
}