因为项目需要对图片进行浏览,而网上一些好的图片浏览框架使用起来比较麻烦,而且需要的代码量较大,于是自行封装了一个图片浏览器。在使用方面我会用一个例子进行说明,而图片浏览的背景可以自定义与该图片浏览器没有关系。个人觉得使用比较方便简单,如果在使用的过程中遇到bug,可以联系1152650004,一起探讨。
首先是进度条,用来显示图片加载的进度,代码如下:
1 // 2 // LMFProgressView.m 3 // LiPu 4 // 5 // Created by wantexe on 16/7/5. 6 // Copyright © 2016年 limingfeng. All rights reserved. 7 // 8 9 #import "LMFProgressView.h" 10 #import <QuartzCore/QuartzCore.h> 11 12 @interface LMFProgressView () 13 @property (nonatomic, strong) UILabel *myLabel; 14 @end 15 16 @implementation LMFProgressView 17 18 - (instancetype)initWithFrame:(CGRect)frame 19 { 20 if (self = [super initWithFrame:frame]) 21 { 22 self.backgroundColor = [UIColor clearColor]; 23 self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)]; 24 self.myLabel.textColor = [UIColor whiteColor]; 25 self.myLabel.textAlignment = NSTextAlignmentCenter; 26 self.myLabel.backgroundColor = [UIColor clearColor]; 27 self.myLabel.center = self.center; 28 self.myLabel.font = [UIFont systemFontOfSize:12]; 29 [self addSubview:self.myLabel]; 30 } 31 return self; 32 } 33 // Only override drawRect: if you perform custom drawing. 34 // An empty implementation adversely affects performance during animation. 35 - (void)drawRect:(CGRect)rect 36 { 37 // 生产出一个圆的路径 -M_PI_2 -M_PI_2+angle*M_PI*2 38 CGPoint circleCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); 39 CGFloat circleRadius = self.bounds.size.width/2.0; 40 double angle = 1.4; 41 if (angle>2) { 42 angle-=2; 43 }else if (angle>1){ 44 angle-=1; 45 } 46 UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:circleCenter 47 radius:circleRadius 48 startAngle:-M_PI_2 49 endAngle:-M_PI_2+2*M_PI*self.progress 50 clockwise:YES]; 51 52 // 生产出一个圆形路径的Layer 53 CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 54 shapeLayer.path = circlePath.CGPath; 55 shapeLayer.strokeColor = [UIColor whiteColor].CGColor; 56 shapeLayer.fillColor = [[UIColor clearColor] CGColor]; 57 shapeLayer.lineWidth = 1; 58 // shapeLayer.lineCap = @"round"; 59 60 // 可以设置出圆的完整性 61 shapeLayer.strokeStart = 0; 62 shapeLayer.strokeEnd = self.progress; 63 [self.layer addSublayer:shapeLayer]; 64 65 } 66 67 - (void)setProgress:(CGFloat)progress 68 { 69 _progress = progress; 70 NSString *text = [NSString stringWithFormat:@"%li%@", (NSInteger)(self.progress*100), @"%"]; 71 self.myLabel.text = text; 72 /* 73 setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到 UIGraphicsGetCurrentContext,就可以画画了。而setNeedsLayout会默认调用layoutSubViews, 74 */ 75 [self setNeedsDisplay]; 76 if (self.progress >= 1) 77 { 78 [self removeFromSuperview]; 79 } 80 } 81 82 @end
接下来是对图片处理的核心代码,主要添加了单击返回事件,双击放大缩小事件,两根手指放大缩小事件,代码如下:
1 // 2 // LMFImageView.m 3 // LiPu 4 // 5 // Created by wantexe on 16/7/4. 6 // Copyright © 2016年 limingfeng. All rights reserved. 7 // 8 /* 9 最多放大为原图的2倍,超过时要自动缩小;最小缩小为原图的一半 10 */ 11 #define LMFBIGRATE 2 12 #define LMFSMALLRATE 0.5 13 #define SCREENW [UIScreen mainScreen].bounds.size.width 14 #define SCREENH [UIScreen mainScreen].bounds.size.height 15 #import "LMFImageView.h" 16 #import "UIImageView+WebCache.h" 17 @interface LMFImageView () 18 { 19 NSInteger gestureTimes; 20 CGRect oringinalFrame; 21 float _totalScale; 22 LMFProgressView *lmfProgressView; 23 } 24 @end 25 26 @implementation LMFImageView 27 28 - (instancetype)initWithFrame:(CGRect)frame 29 { 30 if (self = [super initWithFrame:frame]) 31 { 32 self.userInteractionEnabled = YES; 33 self.multipleTouchEnabled = YES; 34 self.layer.masksToBounds = YES; 35 // self.backgroundColor = [UIColor yellowColor]; 36 gestureTimes = 0; 37 _totalScale = 1; 38 self.imageView = [[UIImageView alloc] init]; 39 self.imageView.center = CGPointMake(SCREENW*0.5, SCREENH*0.5); 40 oringinalFrame = self.imageView.frame; 41 [self addSubview:self.imageView]; 42 43 lmfProgressView = [[LMFProgressView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; 44 lmfProgressView.center = CGPointMake(SCREENW*0.5, SCREENH*0.5); 45 [self addSubview:lmfProgressView]; 46 } 47 return self; 48 } 49 50 #pragma mark --设置图片地址 51 - (void)setPictureUrl:(NSString *)url andDefaultPicture:(NSString *)picture 52 { 53 __weak typeof(lmfProgressView)weakProgress = lmfProgressView; 54 [self.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:picture] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) { 55 weakProgress.progress = (CGFloat)receivedSize/(CGFloat)expectedSize; 56 NSLog(@"%f", weakProgress.progress); 57 } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 58 if (error) 59 { 60 NSLog(@"图片加载失败"); 61 [weakProgress removeFromSuperview]; 62 } 63 if (error.description == nil) 64 { 65 float width = SCREEN_WIDTH; 66 float height = width*image.size.height/image.size.width; 67 self.imageView.frame = CGRectMake(0, 0, width, height); 68 self.imageView.center = CGPointMake(SCREEN_WIDTH*0.5+10, SCREEN_HEIGHT*0.5); 69 [weakProgress removeFromSuperview]; 70 } 71 }]; 72 } 73 74 #pragma mark --否添加两根手指放大缩小的手势 75 - (void)addPinGesture 76 { 77 UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoomImage:)]; 78 [self addGestureRecognizer:pin]; 79 } 80 #pragma mark --用两根手指来改变图片大小 81 - (void)changeFrame:(UIPinchGestureRecognizer *)gesture 82 { 83 CGFloat scale = gesture.scale; 84 CGFloat temp = _totalScale + (scale - 1); 85 [self setTotalScale:temp]; 86 gesture.scale = 1.0; 87 } 88 - (void)zoomImage:(UIPinchGestureRecognizer *)recognizer 89 { 90 CGFloat scale = recognizer.scale; 91 CGFloat temp = _totalScale + (scale - 1); 92 93 [self setTotalScale:temp]; 94 recognizer.scale = 1.0; 95 } 96 97 - (void)setTotalScale:(CGFloat)totalScale 98 { 99 if ((_totalScale < 0.5 && totalScale < _totalScale) || (_totalScale > 3.0 && totalScale > _totalScale)) return; // 最大缩放 2倍,最小0.5倍 100 101 [self zoomWithScale:totalScale]; 102 } 103 104 - (void)zoomWithScale:(CGFloat)scale 105 { 106 _totalScale = scale; 107 self.imageView.transform = CGAffineTransformMakeScale(scale, scale); 108 109 CGRect rect = self.imageView.frame; 110 if (scale > 1) 111 { 112 rect.origin.x = 10; 113 } 114 else 115 { 116 rect.origin.x = (SCREEN_WIDTH-rect.size.width)/2.0+10; 117 } 118 self.imageView.frame = rect; 119 [self setContentSize:CGSizeMake(self.imageView.frame.size.width, self.imageView.frame.size.height)]; 120 121 } 122 #pragma mark --将图片变回到原图大小 123 - (void)changeFrameToOringinal 124 { 125 //变回原图 126 [UIView animateWithDuration:0.3 animations:^{ 127 self.imageView.frame = oringinalFrame; 128 self.imageView.center = CGPointMake(SCREENW*0.5, SCREENH*0.5); 129 }]; 130 } 131 #pragma mark --变回原图的2倍大小 132 - (void)changeframeToTwoTimes 133 { 134 CGRect frame = self.frame; 135 //变大两倍 136 frame.size = CGSizeMake(frame.size.width*LMFBIGRATE, frame.size.height*LMFBIGRATE); 137 [UIView animateWithDuration:0.3 animations:^{ 138 self.imageView.frame = frame; 139 self.imageView.center = CGPointMake(SCREENW*0.5, SCREENH*0.5); 140 }]; 141 } 142 143 #pragma mark --添加单击双击手势 144 - (void)addSingleAndDoubleGesture 145 { 146 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)]; 147 tap.numberOfTapsRequired = 1; 148 // [self addGestureRecognizer:tap]; 149 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigImage:)]; 150 doubleTap.numberOfTapsRequired = 2; 151 [self addGestureRecognizer:doubleTap]; 152 [tap requireGestureRecognizerToFail:doubleTap]; 153 } 154 #pragma mark --单击手势 155 - (void)dismiss:(UITapGestureRecognizer *)gesture 156 { 157 self.block(); 158 } 159 #pragma mark --双击手势 160 - (void)bigImage:(UITapGestureRecognizer *)gesture 161 { 162 CGRect frame = self.imageView.frame; 163 if (gestureTimes%2 == 0) 164 { 165 //变大两倍 166 frame.size = CGSizeMake(frame.size.width*LMFBIGRATE, frame.size.height*LMFBIGRATE); 167 [UIView animateWithDuration:0.3 animations:^{ 168 self.imageView.frame = frame; 169 self.imageView.center = CGPointMake(SCREENW*0.5, SCREENH*0.5); 170 }]; 171 } 172 else 173 { 174 //变回原图 175 [self changeFrameToOringinal]; 176 } 177 gestureTimes++; 178 } 179 #pragma mark --隐藏加载条 180 - (void)hiddenProgress 181 { 182 lmfProgressView.hidden = YES; 183 } 184 185 @end
1 // 2 // LMFImageView.h 3 // LiPu 4 // 5 // Created by wantexe on 16/7/4. 6 // Copyright © 2016年 limingfeng. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 #import "LMFProgressView.h" 11 typedef void(^dismissBlock)(); 12 typedef void(^bigFrameBlock)(CGRect); 13 14 @interface LMFImageView : UIScrollView 15 16 @property (nonatomic, copy) dismissBlock block; 17 @property (nonatomic, copy) bigFrameBlock bigFrameBlock; 18 @property (nonatomic, strong) UIImageView *imageView; 19 #pragma mark --否添加两根手指放大缩小的手势 20 - (void)addPinGesture; 21 #pragma mark --将图片变回到原图大小 22 - (void)changeFrameToOringinal; 23 #pragma mark --添加单击双击手势 24 - (void)addSingleAndDoubleGesture; 25 #pragma mark --设置图片地址 26 - (void)setPictureUrl:(NSString *)url andDefaultPicture:(NSString *)picture; 27 #pragma mark --隐藏加载条 28 - (void)hiddenProgress; 29 @end