UIImageView中图片的大小缩放

@interface HYShowImageView : UIScrollView <UIScrollViewDelegate>

//显示图像大图
-(void)showImage:(UIImage*)image inView:(UIView *)parentsView fromRect:(CGRect)rect;

@end

先看一下头文件,继承UIScrollView,实现UIScrollViewDelegate协议。

提供了一个方法ShowImage。

接下来看看这个方法是实现

-(void)showImage:(UIImage*)image inView:(UIView *)parentsView fromRect:(CGRect)rect
{
    _oldRect = rect;
    
    [self setFrame:CGRectMake(0, 0, PHOTOWIDTH, PHOTOHEIGHT)];
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    
    UIImageView *showView = [[UIImageView alloc] initWithFrame:_oldRect];
    showView.contentMode = UIViewContentModeScaleAspectFit;
    [UIView animateWithDuration:0.5f animations:^{
        [showView setFrame:CGRectMake(0, 0, PHOTOWIDTH, PHOTOHEIGHT)];
    }];
    
    [self setBackgroundColor:color_with_rgba(0, 0, 0, 1)];
    [parentsView addSubview:self];
    
    [showView setTag:'show'];
    [showView setImage:image];   //这个地方也可以用网络的图片
    [self addSubview:showView]; 
    
    //增加两个手势
    showView.userInteractionEnabled = YES;
    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleViewTap:)];
    [self addGestureRecognizer:singleTap];
    
    UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchView:)];
    [self addGestureRecognizer:pinchGesture];
}

增加了两个手势,一个点击取消,一个缩放

//移除图片查看视图
-(void)handleSingleViewTap:(UITapGestureRecognizer *)sender
{
    [self setZoomScale:1.0 animated:NO];
    [UIView animateWithDuration:0.5f animations:^{
        UIImageView *showView = (UIImageView *)[self viewWithTag:'show'];
        showView.frame = _oldRect;
        self.backgroundColor = color_with_rgba(0, 0, 0, 0.0);
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];
}

//缩放图片
-(void)handlePinchView:(UIPinchGestureRecognizer *)sender
{
    UIImageView *imageView = (UIImageView *)[self viewWithTag:'show'];
    if ([sender state] == UIGestureRecognizerStateBegan) {
        _imageHWScale = imageView.image.size.height/imageView.image.size.width;
        _beganScale = self.zoomScale;
    }

    [self setZoomScale:_beganScale * sender.scale];

    if ([sender state] == UIGestureRecognizerStateEnded) {
        [self scrollViewEnd];
    }
}

- (void)scrollViewEnd
{
    if (self.zoomScale < 1.0) {
        [self setZoomScale:1.0 animated:YES];
        self.contentOffset = CGPointMake(0, 0);
    } else if (self.zoomScale > 3.0) {
        [self setZoomScale:3.0 animated:YES];
    }
}

实现委托的方法:

#pragma mark - ScrollView delegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    for (UIView *v in scrollView.subviews){
        return v;
    }
    return nil;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    [self scrollViewEnd];
}·

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    UIImageView *imageView = (UIImageView *)[self viewWithTag:'show'];
    _imageHWScale = imageView.image.size.height/imageView.image.size.width;
    if (self.contentOffset.x<=0 && self.contentOffset.y<=0) {
        self.contentOffset = CGPointMake((imageView.width - PHOTOWIDTH)/2, (imageView.width - PHOTOWIDTH));
    }
}

总结:

优点:这种缩放方式是使用了ScrollView自带的zoom缩放,所有使用简单

缺点:这种缩放方式如果图片高度不符合屏幕的话,放大后上下会有很大的空白部分(待解决)

推荐使用库:MJPhotoBrowse:http://code4app.com/ios/快速集成图片浏览器/525e06116803fa7b0a000001

原版本有一些问题,我修改了bug到了这里:http://git.oschina.net/jonear/MJPhotoBrowser

    原文作者:移动开发
    原文地址: https://my.oschina.net/iq19900204/blog/206816
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞