Objective-C: 从UIScrollView中获取缩放裁剪后的图片

5

场景

我正在开发一款照片分享应用。该应用需要允许用户在将图片发送到服务器之前裁剪图片。我正在使用一个包含我想要用户裁剪的图像的UIScrollView和一个UIImageView子视图。

尝试过的方法

我尝试了一些在StackOverflow上的解决方案,但它们都涉及在用户缩放后拍摄UIScrollView框架的屏幕截图。这不会起作用的原因是当用户在iPhone 5上拍摄照片(320x320,从屏幕宽度的完美正方形),当该照片在显示在iPhone 6 Plus上(较大的屏幕尺寸)时,图像将变得模糊。

问题

如何获得一个可能是3000x3000的图像,在UIScrollView中进行缩放,并提取一个尺寸为UIScrollView框架的图像(320x320),但缩放至640x640而不会丢失图像分辨率?

示例

enter image description here

1个回答

2

首先,您可以创建一个包含UIImageView的UIScrollView子类,在其内部进行初始化,并将图像实例分配给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);

}

我在我的项目中应用了以上所有代码,希望对你有所帮助:)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接