1. 程式人生 > >iOS 圖片的縮放與居中

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