iOS 图片的缩放与居中

@interface ZJCollectionViewCell()<UIScrollViewDelegate>

@end

@implementation ZJCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _scrollView = [[UIScrollView alloc] init];
        
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = YES;
        _scrollView.bouncesZoom = YES;
        _scrollView.minimumZoomScale = 1.0;
        _scrollView.maximumZoomScale = 2.0;
        _scrollView.userInteractionEnabled = YES;
        _scrollView.bounces = NO;
        
        [self.contentView addSubview:_scrollView];
        
        [_scrollView makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.equalTo(0);
        }];
        
        _worksImageView = [[UIImageView alloc] init];
        _worksImageView.clipsToBounds = YES;
        _worksImageView.userInteractionEnabled = YES;
        _worksImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        [_scrollView addSubview:_worksImageView];
    }
    
    return self;
}
- (void)setValue:(ZJWorksDetailsModel *)model
{
    //找到图片名字
    NSArray *strArr = [model.ARTWORK_FILE_ORIGINAL componentsSeparatedByString:@"/"];
    NSString *imageName = [strArr lastObject];
    //得到图片的路径
    NSString *picturePath = [PicturePath(GetData(@"UserID")) stringByAppendingPathComponent:imageName];
    if ([[NSFileManager defaultManager] fileExistsAtPath:picturePath])
    {
        ZJLog(@"本地获取图片");
        _worksImageView.image = [UIImage imageWithContentsOfFile:picturePath];
        
        [self setImage:_worksImageView.image];
    }
    else
    {
        ZJLog(@"网络获取图片");
        NSURL *url = [NSURL URLWithString:[ImageHead stringByAppendingString:model.ARTWORK_FILE_ORIGINAL]];
        [_worksImageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            [self setImage:image];
            
        }];
    }
    
}

#pragma mark - 更改约束
- (void)setImage:(UIImage *)image
{
    //得到缩放比例
    float sclX = self.frame.size.width / image.size.width;
    float sclY = self.frame.size.height / image.size.height;
    
    CGFloat imageHeight = image.size.height * sclX;
    CGFloat imageWidth = self.frame.size.width;
    
    if (sclX > sclY)
    {
        imageWidth = image.size.width * sclY;
        imageHeight = self.frame.size.height;
    }
    
    [_scrollView remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(0);
    }];
    //这里只能用frame不能用约束,否则当缩放时约束会重新调整位置,没有居中
    _worksImageView.frame = (CGRect){self.frame.size.width / 2 - imageWidth / 2, self.frame.size.height / 2 - imageHeight / 2, imageWidth, imageHeight};
//    [_worksImageView remakeConstraints:^(MASConstraintMaker *make) {
//        
//        make.centerX.equalTo(_scrollView.centerX);
//        make.centerY.equalTo(_scrollView.centerY);
//        make.width.equalTo(imageWidth);
//        make.height.equalTo(imageHeight);
//    }];
}
#pragma mark - <UIScrollViewDelegate>
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _worksImageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGSize boundsSize = scrollView.bounds.size;
    CGRect imgFrame = _worksImageView.frame;
    
    CGSize contentSize = scrollView.contentSize;
    //默认在contentSize的中央
    CGPoint centerPoint = CGPointMake(contentSize.width / 2, contentSize.height / 2);
    
    // center horizontally如果图片的宽比scrollView的宽小,那么就用scrollView的宽
    if (imgFrame.size.width <= boundsSize.width)
    {
        centerPoint.x = boundsSize.width / 2;
    }
    
    // center vertically如果图片的宽比scrollView的高小,那么就用scrollView的高
    if (imgFrame.size.height <= boundsSize.height)
    {
        centerPoint.y = boundsSize.height / 2;
    }
    
    _worksImageView.center = centerPoint;
}


@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值