0

I am implementing a image cropper which will crop image captured from the camera. Here is the actual result and expected result of the UI.

See screenshot

I followed this answer to get the desired visible image frame.

And after this, below is my code to crop the image using the visible frame:

func crop(_ rect: CGRect) -> UIImage {
    if imagepicked.scale > 1.0 {
      _ = CGRect(x: CGFloat(rect.origin.x * imagepicked.scale), y: CGFloat(rect.origin.y * imagepicked.scale), width: CGFloat(rect.size.width * imagepicked.scale)*2, height: CGFloat(rect.size.height * imagepicked.scale)*2)
    }
    let imageRef: CGImage? = imagepicked.cgImage?.cropping(to: rect)
    let result = UIImage(cgImage: imageRef!, scale: imagepicked.scale, orientation: imagepicked.imageOrientation)
    return result
} 

but I am not getting the expected result.

Community
  • 1
  • 1
Sumeet Purohit
  • 657
  • 1
  • 7
  • 16
  • http://stackoverflow.com/questions/158914/cropping-an-uiimage/14712184#14712184 – Jing Feb 14 '17 at 07:25
  • I think that's the same code I used, the answer you referred is fixing rotation issue as well but I don't have rotation issue. Could you please tell me some other way. – Sumeet Purohit Feb 14 '17 at 08:34
  • it is cause by orientation. UIImageView automatically show the right direction. but it's wrong when using the crop rect. It only happens to album photos. you may rotate crop rect accordingly( not sure ) or manually translate CTM then crop ( sure but a little complicated ). – Jing Feb 15 '17 at 07:26

0 Answers0