Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically adjust contentInset in scrollViewDidZoom? I initially set contentInset of UIImageView in UIScrollview

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.

screenshot 1

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.

screenshot 2

Black region is contentInset area.

However, if I pinch this scrollView to zoom in, something happen.

screenshot 3

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 :)

like image 856
Wooseong Kim Avatar asked Apr 10 '13 14:04

Wooseong Kim


1 Answers

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.

screenshot 4

like image 92
Wooseong Kim Avatar answered Sep 28 '22 01:09

Wooseong Kim