前言
需求背景:查看收单协议长图,由于图片内容比较多和长的话,需要利用放大图片进行查看。
I 、使用方法
- (void)setupClickPic:(QCTCollectionModel*)m { __weak __typeof__(self) weakSelf = self; CRMImageZoomVCViewController *detail = [[CRMImageZoomVCViewController alloc]init]; CRMimgURLDto *dto =(CRMimgURLDto*) m.subModel; detail.imagePath = dto.originalAddress; detail.image_type = image_type_enum4url; [UIView transitionWithView:self.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [weakSelf.navigationController pushViewController:detail animated:NO]; } completion:nil]; }
- 可修改ZoomScale 调整最大的放大倍数
_picScrollView.maximumZoomScale = 4.8f; _picScrollView.minimumZoomScale = 0.5f;//最小缩放倍数
II、代码实现
- .h
NS_ASSUME_NONNULL_BEGIN typedef enum : NSUInteger { image_type_enum4UIImage, image_type_enum4imageName, image_type_enum4url, } image_type_enum; @interface CRMImageZoomVCViewController : UIViewController @property (nonatomic , assign) image_type_enum image_type; @property (nonatomic , copy) NSString *chuanImageName; @property (nonatomic , copy) NSString *leixingtype; @property (nonatomic , copy) NSString *imagePath; //@property (nonatomic , copy) NSString *type; @property (nonatomic , copy) UIImage *image; @property (nonatomic , strong) UIColor *showImgViewbackgroundColor; @end
.m
// #import "CRMImageZoomVCViewController.h" /** 放大和缩小图片 */ @interface CRMImageZoomVCViewController () { CGRect _firstX; CGRect _firstY; } // *; @property (nonatomic, strong) UIImageView *showImgView; @property (nonatomic, strong) UIScrollView *picScrollView; @end @implementation CRMImageZoomVCViewController - (void)setShowImgViewbackgroundColor:(UIColor *)showImgViewbackgroundColor{ _showImgViewbackgroundColor= showImgViewbackgroundColor; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBarHidden = YES; self.view.backgroundColor = [UIColor blackColor]; [self makeUI]; } - (void)mainBtnDown:(UIButton*)btn { [self.navigationController popViewControllerAnimated:YES]; } // //- (void)setMaximumZoomScale:(CGFloat)maximumZoomScale{ // _maximumZoomScale =maximumZoomScale; // // self.picScrollView.maximumZoomScale =maximumZoomScale; // //} // //- (void)setMinimumZoomScale:(CGFloat)minimumZoomScale{ // // _minimumZoomScale = minimumZoomScale; // self.picScrollView.minimumZoomScale =minimumZoomScale; // //} - (UIScrollView *)picScrollView{ if (nil == _picScrollView) { UIScrollView *tmpView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _picScrollView = tmpView; [self.navigationController.view addSubview:_picScrollView]; _picScrollView.multipleTouchEnabled = YES; _picScrollView.maximumZoomScale = 4.8f; _picScrollView.minimumZoomScale = 0.5f;//最小缩放倍数 _picScrollView.zoomScale = 1.0f; _picScrollView.contentSize = CGSizeMake(kWidth, kHeight); _picScrollView.delegate = self; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)]; _picScrollView.userInteractionEnabled = YES; [_picScrollView addGestureRecognizer:tapGesture]; } return _picScrollView; } - (UIImageView *)showImgView{ if (nil == _showImgView) { // UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight, kWidth, kHeight - kStatusBarHeight)]; UIImageView *tmpView = [[UIImageView alloc]initWithFrame:CGRectMake(0,kStatusBarHeight, holderView.frame.size.width, holderView.frame.size.height - kStatusBarHeight)]; _showImgView = tmpView; [self.picScrollView addSubview:_showImgView]; switch (self.image_type) { case image_type_enum4UIImage: { tmpView.image = _image; } break; case image_type_enum4url: { // [tmpView sd_setImageWithURL:[NSURL URLWithString:self.imagePath]]; [tmpView sd_setImageWithURL:[NSURL URLWithString:self.imagePath] placeholderImage:[UIImage imageNamed:@"占位"]]; } break; case image_type_enum4imageName: { tmpView.image = [UIImage imageNamed:self.imagePath]; } break; default: { tmpView.image = _image; } break; } tmpView.contentMode = UIViewContentModeScaleAspectFit; } return _showImgView; } // - (void)makeUI { [self picScrollView]; [self showImgView]; // [_picScrollView removeFromSuperview]; // // // self.picScrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds]; // self.picScrollView.multipleTouchEnabled = YES; //// self.picScrollView.maximumZoomScale = 2.0f; // //ImageZoomScale // // self.picScrollView.maximumZoomScale = 2.0f; // // self.picScrollView.minimumZoomScale = 0.5f;//最小缩放倍数 // self.picScrollView.zoomScale = 1.0f; // self.picScrollView.contentSize = CGSizeMake(kWidth, kHeight); // self.picScrollView.delegate = self; // [self.navigationController.view addSubview:_picScrollView]; // // // // UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight, kWidth, kHeight - kStatusBarHeight)]; // // showImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,kStatusBarHeight, holderView.frame.size.width, holderView.frame.size.height - kStatusBarHeight)]; // if (_type.length > 0) { // if ([_type isEqualToString:@"yes"]) { // [showImgView sd_setImageWithURL:[NSURL URLWithString:self.imagePath]]; // } else { // showImgView.image = [UIImage imageNamed:self.imagePath]; // } // } else { // showImgView.image = _image; // } // showImgView.contentMode = UIViewContentModeScaleAspectFit; // [self.picScrollView addSubview:showImgView]; // // UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)]; // self.picScrollView.userInteractionEnabled = YES; // [self.picScrollView addGestureRecognizer:tapGesture]; } - (void)tapGestureAction:(UIGestureRecognizer *)gesture { [_picScrollView removeFromSuperview]; [UIView transitionWithView:self.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [self.navigationController popViewControllerAnimated:NO]; } completion:nil]; } - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.showImgView; } - (void)scrollViewDidZoom:(UIScrollView *)scrollView { if (scrollView.zoomScale <= 1.0f) { self.showImgView.center = scrollView.center; } } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; self.navigationController.navigationBarHidden = NO; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.navigationBarHidden = YES; if(self.showImgViewbackgroundColor){ self.showImgView.backgroundColor =self.showImgViewbackgroundColor ; } } @end
see also
iOS解决压缩之后图片模糊的问题