您的位置:首页 > 移动开发 > IOS开发

iOS 图片的缩放与居中

2016-09-29 10:42 281 查看
@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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息