iOS 简单的图片缩放
前言:還是這個圖片瀏覽的小demo,在里面實現了對單張圖片的放大縮小。現在把我已知的圖片縮放的各種方法記錄下來,方便以后查閱,畢竟圖片縮放還是經常用到的。
正文:構建圖像縮略圖,通常有3種形式:保持圖像比例不變調整大小;裁剪部分圖像來匹配可用空間;通過匹配可用空間的長度和寬度來填充圖像。
一、最簡單的方法
UIGraphicsBeginImageContext(size); //size 為CGSize類型,即你所需要的圖片尺寸
[sourceImage drawInRect:newImageRect]; //sourceImage為原圖,newImageRect指定了圖片繪制區域
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
OK,newImage 就是你要的圖。缺點:回丟失很多圖片信息
二、利用UISrcollView的方法
_srcollView.minimumZoomScale=0.5f;//最小縮小倍數
_srcollView.maximumZoomScale=2.0f;//最大方法倍數
// return a view that will be scaled. if delegate returns nil, nothing happen
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(@"viewForZoomingInScrollView");
for (id view in [_srcollView subviews]) {
if ([view isKindOfClass:[UIImageView class]]) {
return view;
}
}
return nil;
}
三、自己寫縮放方法
我們一般會寫一個ImageHelper類,在這個類中實現圖片的尺寸調整等所有功能。先附上代碼:
1 #import <Foundation/Foundation.h> 2 3 #define CGAutorelease(x) (__typeof(x))[NSMakeCollectable(x) autorelease] 4 5 #define SUPPPORTS_UNDOCUMENTED_APIS 1 6 7 @interface ImageHelper : NSObject 8 // Create image 9 + (UIImage *) imageFromView: (UIView *) theView; 10 11 // Base Image Fitting 12 + (CGSize) fitSize: (CGSize)thisSize inSize: (CGSize) aSize; 13 + (UIImage *) unrotateImage: (UIImage *) image; 14 15 + (UIImage *) image: (UIImage *) image fitInSize: (CGSize) size; // retain proportions, fit in size 16 + (UIImage *) image: (UIImage *) image fitInView: (UIView *) view; 17 18 + (UIImage *) image: (UIImage *) image centerInSize: (CGSize) size; // center, no resize 19 + (UIImage *) image: (UIImage *) image centerInView: (UIView *) view; 20 21 + (UIImage *) image: (UIImage *) image fillSize: (CGSize) size; // fill all pixels 22 + (UIImage *) image: (UIImage *) image fillView: (UIView *) view; 23 24 #if SUPPPORTS_UNDOCUMENTED_APIS 25 + (UIImage *) image: (UIImage *) image withOrientation: (UIImageOrientation) orientation; 26 #endif 27 28 //截取view部分圖片 29 + (UIImage *) imageFromViewWithRect:(UIView *)theView rect:(CGRect)rect; 30 @end
ImageHelper.h
1 #import "ImageHelper.h"
2 #import <QuartzCore/QuartzCore.h>
3
4 #if SUPPPORTS_UNDOCUMENTED_APIS
5 @interface UIImage (privateAPISForOrientation)
6 - (id)initWithCGImage:(struct CGImage *)fp8 imageOrientation:(int)fp12;
7 @end
8 #endif
9
10 @implementation ImageHelper
11
12 #pragma mark Create Image
13
14 // Screen shot the view
15 + (UIImage *) imageFromView: (UIView *) theView
16 {
17 UIGraphicsBeginImageContext(theView.frame.size);
18 CGContextRef context = UIGraphicsGetCurrentContext();
19 [theView.layer renderInContext:context];
20 UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
21 UIGraphicsEndImageContext();
22 return theImage;
23 }
24
25 #pragma mark Base Image Utility
26 + (CGSize) fitSize: (CGSize)thisSize inSize: (CGSize) aSize
27 {
28 CGFloat scale;
29 CGSize newsize = thisSize;
30
31
32 if (newsize.height && (newsize.height > aSize.height))//如果新圖的高度不為0,且大于原圖高度
33 {
34 scale = aSize.height / newsize.height;//比例為 原高/新高
35 newsize.width *= scale;//新圖的寬度等于 =
36 newsize.height *= scale;
37 }
38
39 if (newsize.width && (newsize.width >= aSize.width))
40 {
41 scale = aSize.width / newsize.width;
42 newsize.width *= scale;
43 newsize.height *= scale;
44 }
45
46 return newsize;
47 }
48
49 #define MIRRORED ((image.imageOrientation == UIImageOrientationUpMirrored) || (image.imageOrientation == UIImageOrientationLeftMirrored) || (image.imageOrientation == UIImageOrientationRightMirrored) || (image.imageOrientation == UIImageOrientationDownMirrored))
50 #define ROTATED90 ((image.imageOrientation == UIImageOrientationLeft) || (image.imageOrientation == UIImageOrientationLeftMirrored) || (image.imageOrientation == UIImageOrientationRight) || (image.imageOrientation == UIImageOrientationRightMirrored))
51
52 + (UIImage *) doUnrotateImage: (UIImage *) image
53 {
54 CGSize size = image.size;
55 if (ROTATED90) size = CGSizeMake(image.size.height, image.size.width);
56
57 UIGraphicsBeginImageContext(size);
58 CGContextRef context = UIGraphicsGetCurrentContext();
59 CGAffineTransform transform = CGAffineTransformIdentity;
60
61 // Rotate as needed
62 switch(image.imageOrientation)
63 {
64 case UIImageOrientationLeft:
65 case UIImageOrientationRightMirrored:
66 transform = CGAffineTransformRotate(transform, M_PI / 2.0f);
67 transform = CGAffineTransformTranslate(transform, 0.0f, -size.width);
68 size = CGSizeMake(size.height, size.width);
69 CGContextConcatCTM(context, transform);
70 break;
71 case UIImageOrientationRight:
72 case UIImageOrientationLeftMirrored:
73 transform = CGAffineTransformRotate(transform, -M_PI / 2.0f);
74 transform = CGAffineTransformTranslate(transform, -size.height, 0.0f);
75 size = CGSizeMake(size.height, size.width);
76 CGContextConcatCTM(context, transform);
77 break;
78 case UIImageOrientationDown:
79 case UIImageOrientationDownMirrored:
80 transform = CGAffineTransformRotate(transform, M_PI);
81 transform = CGAffineTransformTranslate(transform, -size.width, -size.height);
82 CGContextConcatCTM(context, transform);
83 break;
84 default:
85 break;
86 }
87
88
89 if (MIRRORED)
90 {
91 // de-mirror
92 transform = CGAffineTransformMakeTranslation(size.width, 0.0f);
93 transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
94 CGContextConcatCTM(context, transform);
95 }
96
97 // Draw the image into the transformed context and return the image
98 [image drawAtPoint:CGPointMake(0.0f, 0.0f)];
99 UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
100 UIGraphicsEndImageContext();
101 return newimg;
102 }
103
104 + (UIImage *) unrotateImage: (UIImage *) image
105 {
106 if (image.imageOrientation == UIImageOrientationUp) return image;
107 return [ImageHelper doUnrotateImage:image];
108 }
109
110
111
112 // Proportionately resize, completely fit in view, no cropping
113 + (UIImage *) image: (UIImage *) image fitInSize: (CGSize) viewsize
114 {
115 // calculate the fitted size
116 CGSize size = [ImageHelper fitSize:image.size inSize:viewsize];
117
118 UIGraphicsBeginImageContext(viewsize);
119
120 float dwidth = (viewsize.width - size.width) / 2.0f;
121 float dheight = (viewsize.height - size.height) / 2.0f;
122
123 CGRect rect = CGRectMake(dwidth, dheight, size.width, size.height);
124 [image drawInRect:rect];
125
126 UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
127 UIGraphicsEndImageContext();
128
129 return newimg;
130 }
131
132 + (UIImage *) image: (UIImage *) image fitInView: (UIView *) view
133 {
134 return [self image:image fitInSize:view.frame.size];
135 }
136
137 // No resize, may crop
138 + (UIImage *) image: (UIImage *) image centerInSize: (CGSize) viewsize
139 {
140 CGSize size = image.size;
141
142 UIGraphicsBeginImageContext(viewsize);
143 float dwidth = (viewsize.width - size.width) / 2.0f;
144 float dheight = (viewsize.height - size.height) / 2.0f;
145
146 CGRect rect = CGRectMake(dwidth, dheight, size.width, size.height);
147 [image drawInRect:rect];
148
149 UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
150 UIGraphicsEndImageContext();
151
152 return newimg;
153 }
154
155 + (UIImage *) image: (UIImage *) image centerInView: (UIView *) view
156 {
157 return [self image:image centerInSize:view.frame.size];
158 }
159
160 // Fill every view pixel with no black borders, resize and crop if needed
161 + (UIImage *) image: (UIImage *) image fillSize: (CGSize) viewsize
162
163 {
164 CGSize size = image.size;
165
166 CGFloat scalex = viewsize.width / size.width;
167 CGFloat scaley = viewsize.height / size.height;
168 CGFloat scale = MAX(scalex, scaley);
169
170 UIGraphicsBeginImageContext(viewsize);
171
172 CGFloat width = size.width * scale;
173 CGFloat height = size.height * scale;
174
175 float dwidth = ((viewsize.width - width) / 2.0f);
176 float dheight = ((viewsize.height - height) / 2.0f);
177
178 CGRect rect = CGRectMake(dwidth, dheight, size.width * scale, size.height * scale);
179 [image drawInRect:rect];
180
181 UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
182 UIGraphicsEndImageContext();
183
184 return newimg;
185 }
186
187 + (UIImage *) image: (UIImage *) image fillView: (UIView *) view
188 {
189 return [self image:image fillSize:view.frame.size];
190 }
191
192 #if SUPPPORTS_UNDOCUMENTED_APIS
193 + (UIImage *) image: (UIImage *) image withOrientation: (UIImageOrientation) orientation
194 {
195 UIImage *newimg = [[UIImage alloc] initWithCGImage:[image CGImage] imageOrientation:orientation];
196 return [newimg autorelease];
197 }
198 #endif
199
200 //截取view部分圖片
201 + (UIImage *) imageFromViewWithRect:(UIView *)theView rect:(CGRect)rect
202 {
203 CGFloat scale = 1.0f;
204 if (UIGraphicsBeginImageContextWithOptions != NULL)
205 {
206 UIGraphicsBeginImageContextWithOptions(theView.bounds.size, NO, theView.layer.contentsScale);
207 scale = theView.layer.contentsScale;
208 }
209 else
210 {
211 UIGraphicsBeginImageContext(theView.bounds.size);
212 }
213
214 CALayer * curLayer = [theView layer];
215 CGContextRef context = UIGraphicsGetCurrentContext();
216 [curLayer renderInContext:context];
217
218 UIImage * pImage = UIGraphicsGetImageFromCurrentImageContext();
219 CGImageRef imageRef = pImage.CGImage;
220 CGRect rectSubImage = {rect.origin.x*scale,rect.origin.y*scale,rect.size.width*scale,rect.size.height*scale};
221 CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, rectSubImage);
222 rect.size.width *=scale;
223 rect.size.height *=scale;
224 CGSize size = rect.size;
225 UIGraphicsBeginImageContext(size);
226 CGContextRef subcontext = UIGraphicsGetCurrentContext();
227 CGContextDrawImage(subcontext, rect, subImageRef);
228
229 UIImage * image = [UIImage imageWithCGImage:subImageRef scale:scale orientation:UIImageOrientationUp];
230 UIGraphicsEndImageContext();
231
232 CGImageRelease(subImageRef); //這里不必釋放
233 UIGraphicsEndImageContext();
234
235 return image;
236 }
237 @end
ImageHelper.m
這份代碼iphone開發秘籍這本書中給的,實現了開頭介紹的3種縮放方法。下面會詳細的分析這份代碼。
+(CGSize)fitSize:(CGSize)thisSizeinSize:(CGSize)aSize; 計算出合適的尺寸
+(UIImage*)image:(UIImage*)imagefitInSize:(CGSize)size;成比例縮放,不裁剪,需重新測量尺寸
+(UIImage*)image:(UIImage*)imagecenterInSize:(CGSize)size;不需要再重新測量,可能裁剪
+(UIImage*)image:(UIImage*)imagecenterInSize:(CGSize)size;可能需要裁剪和重新測量,每個像素都會被使用
開始逐個介紹每一種縮放方法究竟是怎么實現的:
1、fitSize: (CGSize)thisSize inSize: (CGSize) aSize;第一個參數是原圖尺寸,第二個是新View的尺寸
根據原圖的尺寸,成比例的計算出新圖的尺寸。
新圖尺寸初始化:CGSize newSize = thisSize;先令新圖的尺寸等于原圖的尺寸
計算縮放比例:原W/原H = 新W/新H
1、fitInSize的實現
image: (UIImage *) image fitInSize: (CGSize) size; 第一個參數是原圖,第二個參數是要構建的新圖的尺寸
成比例的縮放自然要先
待寫。。。
總結
以上是生活随笔為你收集整理的iOS 简单的图片缩放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tween(补间)动画
- 下一篇: 机器翻译评价指标 — BLEU算法