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

情景

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

我做了什么

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

如何在UIScrollView中缩放3000×3000的图像,并提取UIScrollView框架(320×320)尺寸的图像,但缩放到640×640而不会丢失图像分辨率?

插图

enter image description here

最佳答案
首先,您可以将包含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);

}

以上代码我已经在我的项目中应用,希望可能对你有用:)

转载注明原文:ios – Objective-C:从UIScrollView获取缩放的裁剪图像 - 代码日志