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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 基于jquery实现图片相关操作(重绘、获取尺寸、调整大小、缩放)
- php实现图片缩放功能类
- 利用javascript解决图片缩放及其优化的代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题