UIImageView自适应图片大小

  1 窗口大小获取:
  2 
  3 CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect
  4 
  5 CGRect rect = [ [UIScreenmainScreen]applicationFrame];//不包含状态栏的Rect
  6 
  7 UIImageView:
  8 
  9 一 :圆角以及自适应图片大小
 10 
 11  UIImage* image = [UIImage imageNamed:@"image.png"];
 12 
 13  UIImageView* imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
 14 
 15  imageView.frame = CGRectMake(0, 0, 300, 200);
 16 
 17 imageView.layer.cornerRadius = 8;
 18 
 19  imageView.layer.masksToBounds = YES;
 20 
 21 //自适应图片宽高比例
 22 
 23 imageView1.contentMode = UIViewContentModeScaleAspectFit;
 24 
 25 二 图片自适应UIImageView
 26 
 27 - (UIImage *)rescaleImageToSize:(CGSize)size {
 28 
 29 CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
 30 
 31 UIGraphicsBeginImageContext(rect.size);
 32 
 33 [self drawInRect:rect];  // scales image to rect
 34 
 35 UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
 36 
 37 UIGraphicsEndImageContext();
 38 
 39 return resImage;
 40 
 41 }
 42 
 43 - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;   //图片缩放裁剪
 44 
 45 - (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height; //改变大小
 46 
 47 + (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并图片
 48 
 49 + (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect; //裁剪部分图片
 50 
 51 + (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;  //保存图片到媒体库
 52 
 53 关于图片缩放的线程安全和非线程安全操作.
 54 
 55 非线程安全的操作只能在主线程中进行操作,对于大图片的处理肯定会消耗大量的时间,如下面的方法
 56 
 57 方法 1: 使用 UIKit
 58 
 59 + (UIImage*)imageWithImage :( UIImage*)image scaledToSize :(CGSize)newSize;
 60 {
 61 // Create a graphics image context
 62 UIGraphicsBeginImageContext(newSize);
 63 
 64 // Tell the old image to draw in this new context, with the desired
 65 // new size
 66 [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
 67 
 68 // Get the new image from the context
 69 UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
 70 
 71 // End the context
 72 UIGraphicsEndImageContext();
 73 
 74 // Return the new image.
 75 return newImage;
 76 
 77 }
 78 
 79 此方法很简单, 但是,这种方法不是线程安全的情况下.
 80 
 81 方法 2: 使用 CoreGraphics
 82 
 83 + (UIImage*)imageWithImage :( UIImage*)sourceImage scaledToSize :(CGSize)newSize;
 84 {
 85 
 86 
 87 CGFloat targetWidth = targetSize.width;
 88 CGFloat targetHeight = targetSize.height;
 89 CGImageRef imageRef = [sourceImage CGImage];
 90 CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
 91 CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
 92 if (bitmapInfo == kCGImageAlphaNone) {
 93 bitmapInfo = kCGImageAlphaNoneSkipLast;
 94 
 95 }
 96 
 97 CGContextRef bitmap;
 98 
 99 if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {
100 bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo)
101 
102 } else {
103 
104 bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
105 }
106 
107 if (sourceImage.imageOrientation == UIImageOrientationLeft) {
108 CGContextRotateCTM (bitmap, radians(90));
109 CGContextTranslateCTM (bitmap, 0, -targetHeight);
110 
111 } else if (sourceImage.imageOrientation ==UIImageOrientationRight) {
112 CGContextRotateCTM (bitmap, radians(-90));
113 CGContextTranslateCTM (bitmap, -targetWidth, 0);
114 
115 } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
116 
117 // NOTHING
118 
119 } else if (sourceImage.imageOrientation == UIImageOrientationDown){
120 
121 CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
122 CGContextRotateCTM (bitmap, radians(-180.));
123 
124 }
125 
126 CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth,targetHeight), imageRef);
127 CGImageRef ref = CGBitmapContextCreateImage(bitmap);
128 UIImage* newImage = [UIImage imageWithCGImage:ref];
129 CGContextRelease(bitmap);
130 CGImageRelease(ref);
131 return newImage;
132 }
133 
134 这种方法的好处是它是线程安全,加上它负责的 (使用正确的颜色空间和位图信息,处理图像方向) 的小东西,UIKit 版本不会。
135 如何调整和保持长宽比 (如 AspectFill 选项)?
136 它是非常类似于上述,方法,它看起来像这样:
137 
138 + (UIImage*)imageWithImage :( UIImage*)sourceImage scaledToSizeWithSameAspectRatio :( CGSize)targetSize;
139 {
140 CGSize imageSize = sourceImage.size;
141 CGFloat width = imageSize.width;
142 CGFloat height = imageSize.height;
143 CGFloat targetWidth = targetSize.width;
144 CGFloat targetHeight = targetSize.height;
145 CGFloat scaleFactor = 0.0;
146 CGFloat scaledWidth = targetWidth;
147 CGFloat scaledHeight = targetHeight;
148 CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
149 if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
150 CGFloat widthFactor = targetWidth / width;
151 CGFloat heightFactor = targetHeight / height;
152 if (widthFactor > heightFactor) {
153 scaleFactor = widthFactor; // scale to fit height
154 }
155 else {
156 scaleFactor = heightFactor; // scale to fit width
157 }
158 scaledWidth  = width * scaleFactor;
159 scaledHeight = height * scaleFactor;
160 // center the image
161 if (widthFactor > heightFactor) {
162 thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
163 }
164 else if (widthFactor < heightFactor) {
165 thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
166 }
167 }
168 CGImageRef imageRef = [sourceImage CGImage];
169 CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
170 CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
171 if (bitmapInfo == kCGImageAlphaNone) {
172 bitmapInfo = kCGImageAlphaNoneSkipLast;
173 }
174 CGContextRef bitmap;
175 if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {
176 bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
177 } else {
178 bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
179 }
180 
181 
182 // In the right or left cases, we need to switch scaledWidth and scaledHeight,
183 // and also the thumbnail point
184 if (sourceImage.imageOrientation == UIImageOrientationLeft) {
185 thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
186 CGFloat oldScaledWidth = scaledWidth;
187 scaledWidth = scaledHeight;
188 scaledHeight = oldScaledWidth;
189 CGContextRotateCTM (bitmap, radians(90));
190 CGContextTranslateCTM (bitmap, 0, -targetHeight);
191 } else if (sourceImage.imageOrientation ==UIImageOrientationRight) {
192 thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
193 CGFloat oldScaledWidth = scaledWidth;
194 scaledWidth = scaledHeight;
195 scaledHeight = oldScaledWidth;
196 CGContextRotateCTM (bitmap, radians(-90));
197 CGContextTranslateCTM (bitmap, -targetWidth, 0);
198 } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
199 
200 // NOTHING
201 
202 } else if (sourceImage.imageOrientation == UIImageOrientationDown){
203 CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
204 CGContextRotateCTM (bitmap, radians(-180.));
205 }
206 
207 CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x,thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
208 CGImageRef ref = CGBitmapContextCreateImage(bitmap);
209 UIImage* newImage = [UIImage imageWithCGImage:ref];
210 CGContextRelease(bitmap);
211 CGImageRelease(ref);
212 return newImage;
213 }
214 
215  

 

posted @ 2015-11-11 14:39  NingSpeals  阅读(457)  评论(0编辑  收藏  举报