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

时间:2015-10-22 19:59:37

标签: ios objective-c image-processing uiscrollview

SCENARIO

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

我做了什么

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

问题

如何在UIScrollView范围内放大3000x3000放大的图像并提取UIScrollView帧尺寸(320x320)缩放的图像到640x640 没有会丢失图像分辨率?

例证

enter image description here

1 个答案:

答案 0 :(得分:2)

首先,您可以将包含UIImageView的UIScrollView子类化,启动此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'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);

}

我已在我的项目中应用的所有上述代码,希望可能对您有用:)