I recently had a project where I had to crop a UIImageView based on it’s scale, after having been resized within a UIScrollView. The solution to this task turned out to be incredibly simple – see the code below.
imageScrollView – reference to our UIScrollView
editingImage – reference to our UIImage used to create our UIImageView which itself is a child of imageScrollView.
Happy coding 🙂
var scale = 1 / imageScrollView.zoomScale
var visibleRect = CGRectMake(imageScrollView.contentOffset.x * scale, imageScrollView.contentOffset.y*scale, imageScrollView.bounds.size.width*scale, imageScrollView.bounds.size.height*scale)
var ref:CGImageRef = CGImageCreateWithImageInRect(editingImage.CGImage, visibleRect)
var croppedImage:UIImage = UIImage(CGImage: ref)!