iOS 利用UIScrollView实现图片放大预览,并支持缩小

简介: iOS 利用UIScrollView实现图片放大预览,并支持缩小

前言

需求背景:查看收单协议长图,由于图片内容比较多和长的话,需要利用放大图片进行查看。

image.png

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解决压缩之后图片模糊的问题

目录
打赏
0
0
0
0
5
分享
相关文章
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
99 1
iOS TextView插入表情或者图片后字体变大或变小
iOS TextView插入表情或者图片后字体变大或变小
154 1
基于iOS平台的高效图片缓存策略实现
【4月更文挑战第22天】 在移动应用开发中,图片资源的加载与缓存是影响用户体验的重要因素之一。尤其对于iOS平台,由于设备存储空间的限制以及用户对流畅性的高要求,设计一种合理的图片缓存策略显得尤为关键。本文将探讨在iOS环境下,如何通过使用先进的图片缓存技术,包括内存缓存、磁盘缓存以及网络请求的优化,来提高应用的性能和响应速度。我们将重点分析多级缓存机制的设计与实现,并对可能出现的问题及其解决方案进行讨论。
实现iOS平台的高效图片缓存策略
【4月更文挑战第22天】在移动应用开发中,图片资源的处理是影响用户体验的重要因素之一。特别是对于图像资源密集型的iOS应用,如何有效地缓存图片以减少内存占用和提升加载速度,是开发者们面临的关键挑战。本文将探讨一种针对iOS平台的图片缓存策略,该策略通过结合内存缓存与磁盘缓存的机制,并采用先进的图片解码和异步加载技术,旨在实现快速加载的同时,保持应用的内存效率。
实现iOS平台的高效图片缓存策略
【4月更文挑战第23天】在移动应用开发领域,尤其是图像处理密集型的iOS应用中,高效的图片缓存策略对于提升用户体验和节省系统资源至关重要。本文将探讨一种针对iOS平台设计的图片缓存方案,该方案通过结合内存缓存与磁盘缓存的多层次结构,旨在优化图片加载性能并降低内存占用。我们将深入分析其设计理念、核心组件以及在实际场景中的应用效果,同时对比其他常见缓存技术的优势与局限。
iOS页面布局:UIScrollView的布局问题
iOS页面布局:UIScrollView的布局问题
160 8
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
812 1
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
|
10月前
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
74 0
实现iOS平台的高效图片缓存策略
【4月更文挑战第4天】在移动应用开发中,图片资源的加载与缓存是影响用户体验的关键因素之一。尤其对于iOS平台,由于设备存储和内存资源的限制,设计一个高效的图片缓存机制尤为重要。本文将深入探讨在iOS环境下,如何通过技术手段实现图片的高效加载与缓存,包括内存缓存、磁盘缓存以及网络层面的优化,旨在为用户提供流畅且稳定的图片浏览体验。

热门文章

最新文章

  • 1
    苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
    61
  • 2
    苹果app上架app store 之苹果开发者账户在mac电脑上如何使用钥匙串访问-发行-APP发布证书ios_distribution.cer-优雅草卓伊凡
    47
  • 3
    uniapp云打包ios应用证书的获取方法,生成指南
    50
  • 4
    iOS|解决 setBrightness 调节屏幕亮度不生效的问题
    126
  • 5
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    30
  • 6
    iOS各个证书生成细节
    45
  • 7
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    220
  • 8
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    71
  • 9
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    87
  • 10
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    69