SCENARIO
I am working on a photo sharing app. The app needs to allow users to crop the image before they send the image to the server. I am using a UIScrollView
with a UIImageView
subview that contains the image I want the user to crop.
WHAT I'VE TRIED
I have tried to do this with a few solutions on StackOverflow but they involve taking a screen shot of the UIScrollView
frame after the user zooms. The reason this will not work is because when the user takes a screen shot on the iPhone 5 (320x320, perfect square from width of screen), when that photo is uploaded and displayed on a iPhone 6 Plus (much larger screen size), the image will be pixelated.
QUESTION
How do you get an image that may be 3000x3000 zoomed within a UIScrollView
and extract an image that is the dimensions of the UIScrollView
frame (320x320) but is scaled to 640x640 without losing the image resolution?
ILLUSTRATION
First, you can subclass UIScrollView that contain a UIImageView inside it,init this UIScrollView and UIImageView,assgin a image instace to UIImageView,so it can display.
- (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];
}
Second, you can handle in viewForZoomingInScrollView and scrollViewDidZoom,code as below:
- (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's 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);
}
all above code that i have apply in my project,hope that maybe available to you :)
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