Objective-C:从UIScrollView获取缩放的裁剪图像

Bra*_*n A 5 image-processing objective-c uiscrollview ios

情景

我正在制作照片分享应用.该应用程序需要允许用户在将图像发送到服务器之前裁剪图像.我正在使用包含我希望用户裁剪的图像UIScrollViewUIImageView子视图.

我做了什么

我试图在StackOverflow上使用一些解决方案来做到这一点,但它们涉及UIScrollView在用户缩放后拍摄帧的屏幕截图.这不起作用的原因是因为当用户在iPhone 5(320x320,屏幕宽度上的完美正方形)上进行屏幕截图时,当该照片上传并显示在iPhone 6 Plus(更大的屏幕尺寸)上时,图像将像素化.

如何获得可能在3000x3000内放大UIScrollView的图像并提取图像(图像为UIScrollView320x320),缩放到640x640 而不会丢失图像分辨率?

插图

在此输入图像描述

Tho*_*Lee 2

首先,你可以子类化UIScrollView,其中包含一个UIImageView,初始化这个UIScrollView和UIImageView,将图像实例分配给UIImageView,以便它可以显示。

\n\n
- (instancetype)initWithFrame:(CGRect)frame{\n    self = [super initWithFrame:frame];\n    if (self) {\n        self.delegate = self;\n        self.maximumZoomScale = 4.0f;\n        self.showsHorizontalScrollIndicator = NO;\n        self.showsVerticalScrollIndicator = NO;\n        [self loadImageView:frame];\n    }\n    return self;\n}\n\n- (void) loadImageView:(CGRect) frame {\n    if (!_imageView) {\n        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,(frame.size.height-frame.size.width)/2, frame.size.width, frame.size.width)];\n        _imageView.contentMode = UIViewContentModeScaleAspectFit;\n        _imageView.center = self.center;\n        [self addSubview:_imageView];\n    }\n}\n\n//call this menthod to assginment image to imageView\n- (void)showImage:(UIImage*)image{\n    self.imageView.image = image;\n    [self handleImageType];\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

其次,可以在viewForZoomingInScrollView和scrollViewDidZoom中进行处理,代码如下:

\n\n
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {\n\n    return _imageView;//your imageView inside UIScrollView\n}  \n\n\n- (void)scrollViewDidZoom:(UIScrollView *)scrollView\n{\n    CGSize boundSize = self.bounds.size;//scrollView bounds\n    CGFloat boundWidth = boundSize.width;\n    CGFloat boundHeight = boundSize.height;\n\n    CGSize imageSize = self.imageView.image.size;\n    CGFloat imageWidth = imageSize.width;\n    CGFloat imageHeight = imageSize.height;\n\n    CGFloat zoomScale = scrollView.zoomScale;\n    DLog(@"zoomScale === %f",zoomScale);\n\n    [self normalScaleViewDidZoom:boundWidth boundHeight:boundHeight imageWidth:imageWidth imageHeight:imageHeight scrollView:scrollView];\n}  \n\n- (void)handleImageType{\n\n    CGSize imageSize = self.imageView.image.size;//imageView\'s image size\n    CGFloat imageWidth = imageSize.width;\n    CGFloat imageHeight = imageSize.height;\n    if (imageWidth > imageHeight) {\n      type = 1;\n    } else if (imageWidth < imageHeight){\n      type = 2;\n    } else if (imageWidth == imageHeight) {\n      type = 3;\n    }\n}\n- (void)normalScaleViewDidZoom:(CGFloat)boundWidth boundHeight:(CGFloat)boundHeight imageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight scrollView:(UIScrollView*)scrollView{\n\n    CGFloat zoomScale = scrollView.zoomScale;\n    if (type == 1) {\n\n        self.contentSize = CGSizeMake(boundWidth/imageHeight*imageWidth*zoomScale,boundWidth*zoomScale + (boundHeight-boundWidth));\n    } else if (type == 2){\n\n        self.contentSize = CGSizeMake(boundWidth*zoomScale,boundWidth/imageWidth*imageHeight*zoomScale + (boundHeight-boundWidth) );\n    } else if (type == 3) {\n\n        self.contentSize = CGSizeMake(boundWidth*zoomScale,boundHeight-boundWidth+boundWidth*zoomScale);\n    }\n    [self zoomCenter:scrollView];\n}\n\n\n- (void)zoomCenter:(UIScrollView*)scrollView{\n    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;\n    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;\n    self.imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在我的项目中应用了上述所有代码,希望您可以使用 \xef\xbc\x9a\xef\xbc\x89

\n