I'm on a project that makes an custom 'Move and Scale' Controller
of UIImagePickerController
. (controller when appear if imagePickerController.allowsEditing=YES
)
I want to crop an UIImage
in the crop rectangular like the picture below.
And I made some code to set contentInset
.
self.selectedImageView.backgroundColor = [UIColor redColor];
CGRect cropRect = [SKPhotoCropFrameView getCropRectFromOrientation:self.photoCropOrientation];
CGRect aspectFitRect = AVMakeRectWithAspectRatioInsideRect(self.selectedImageView.image.size, self.selectedImageView.frame);
CGFloat difference = fabsf(cropRect.size.height - aspectFitRect.size.height);
self.contentInset = UIEdgeInsetsMake(difference/2, 0, difference/2 + 20, 0); // 20 is status bar height
and here is the result.
Black region is contentInset area.
However, if I pinch this scrollView to zoom in, something happen.
I think I have to do something on
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
to adjust contentInset dynamically. How can I do this? Please give me some help :)
I hoped someone answered this question, I'm sad because it's not.
But thank God, I solved this.
in
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
adjust contentInset dynamically using zoomScale. I just implement only Lanscape mode
for testing, but Portrait mode
is exactly the same way.
// adjust contentInset
CGFloat increasingZoomScale = (scrollView.zoomScale == 1) ? 0 : (-1 * (1 - scrollView.zoomScale));
CGRect cropRect = [SKPhotoCropFrameView getCropRectFromOrientation:self.photoCropOrientation];
CGRect aspectFitRect = AVMakeRectWithAspectRatioInsideRect(self.selectedImageView.image.size, self.selectedImageView.frame);
CGFloat difference = fabsf(cropRect.size.height - aspectFitRect.size.height);
// implement at `Landscape` mode first
if (self.photoCropOrientation == SKPhotoCropOrientationPortrait) {
}else{
// get scaledFrameHeight because it's `Landscape` crop mode
CGFloat increasingFrameHeight = scrollView.frame.size.height * increasingZoomScale;
self.contentInset = UIEdgeInsetsMake(difference/2 - increasingFrameHeight/2, 0, difference/2 - increasingFrameHeight/2, 0);
}
And bam. here is the screenshot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With