Objective-C: get scaled cropped image from UIScrollView

SCENARIO

I am working on a photo sharing application. The application should allow users to crop the image before sending the image to the server. I use UIScrollViewwith the subtext UIImageViewthat contains the image that I want the user to crop.

WHAT I GOT

I tried to do this with several solutions in StackOverflow, but they require taking a snapshot UIScrollViewafter scaling the user. The reason this will not work is because when the user takes a screen shot on the iPhone 5 (320x320, a perfect square across the width of the screen) when this photo is downloaded and displayed on the iPhone 6 Plus (a much larger screen size), the image will be uneven.

Question

How do you get an image that can be 3000x3000 zoomed in UIScrollViewand extract an image that is a frame size UIScrollView(320x320) but scales to 640x640 without losing image resolution?

ILLUSTRATIONS

enter image description here

+4
1

-, UIScrollView, UIImageView , UIScrollView UIImageView, UIImageView, .

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.maximumZoomScale = 4.0f;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        [self loadImageView:frame];
    }
    return self;
}

- (void) loadImageView:(CGRect) frame {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,(frame.size.height-frame.size.width)/2, frame.size.width, frame.size.width)];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        _imageView.center = self.center;
        [self addSubview:_imageView];
    }
}

//call this menthod to assginment image to imageView
- (void)showImage:(UIImage*)image{
    self.imageView.image = image;
    [self handleImageType];
}

-, viewForZoomingInScrollView scrollViewDidZoom , :

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    return _imageView;//your imageView inside UIScrollView
}  


- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGSize boundSize = self.bounds.size;//scrollView bounds
    CGFloat boundWidth = boundSize.width;
    CGFloat boundHeight = boundSize.height;

    CGSize imageSize = self.imageView.image.size;
    CGFloat imageWidth = imageSize.width;
    CGFloat imageHeight = imageSize.height;

    CGFloat zoomScale = scrollView.zoomScale;
    DLog(@"zoomScale === %f",zoomScale);

    [self normalScaleViewDidZoom:boundWidth boundHeight:boundHeight imageWidth:imageWidth imageHeight:imageHeight scrollView:scrollView];
}  

- (void)handleImageType{

    CGSize imageSize = self.imageView.image.size;//imageView image size
    CGFloat imageWidth = imageSize.width;
    CGFloat imageHeight = imageSize.height;
    if (imageWidth > imageHeight) {
      type = 1;
    } else if (imageWidth < imageHeight){
      type = 2;
    } else if (imageWidth == imageHeight) {
      type = 3;
    }
}
- (void)normalScaleViewDidZoom:(CGFloat)boundWidth boundHeight:(CGFloat)boundHeight imageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight scrollView:(UIScrollView*)scrollView{

    CGFloat zoomScale = scrollView.zoomScale;
    if (type == 1) {

        self.contentSize = CGSizeMake(boundWidth/imageHeight*imageWidth*zoomScale,boundWidth*zoomScale + (boundHeight-boundWidth));
    } else if (type == 2){

        self.contentSize = CGSizeMake(boundWidth*zoomScale,boundWidth/imageWidth*imageHeight*zoomScale + (boundHeight-boundWidth) );
    } else if (type == 3) {

        self.contentSize = CGSizeMake(boundWidth*zoomScale,boundHeight-boundWidth+boundWidth*zoomScale);
    }
    [self zoomCenter:scrollView];
}


- (void)zoomCenter:(UIScrollView*)scrollView{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    self.imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);

}

, , , :)

+2

Source: https://habr.com/ru/post/1612829/


All Articles