How can I dynamically set contentInset in scrollViewDidZoom? I originally set the contentInset UIImageView to UIScrollview

I am in a project that creates a custom 'Move and Scale' Controller from UIImagePickerController . (the controller appears if imagePickerController.allowsEditing=YES )

I want crop an UIImage in the crop rectangle, as shown in the image below.

screenshot 1

And I made the code to set the 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

The black region is the content area.

However, if I pinch this scrollView to increase, something will happen.

screenshot 3

I think I need to do something on

  - (void)scrollViewDidZoom:(UIScrollView *)scrollView 

to change content dynamically. How can i do this? Please help me:)

+6
source share
1 answer

I was hoping that someone answered this question, I am sad because it is not.

But, thank God, I decided that.

in

  - (void)scrollViewDidZoom:(UIScrollView *)scrollView 

customize content Selected dynamically using scaling. I just implement only Lanscape mode for testing, but Portrait mode exactly the same.

 // 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 `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 a screenshot.

screenshot 4

+3
source

All Articles