Fork me on GitHub

图片缩放的三个函数

程序中一个界面用到了好多张大图,内存报警告了,所以做了一下图片缩放,在网上找了别人写的代码

//把图片做等比缩放,生成一个新图片
 1 - (UIImage *) imageByScalingProportionallyToSize:(CGSize)targetSize sourceImage:(UIImage *)sourceImage {
2
3 UIGraphicsBeginImageContext(targetSize);
4 [sourceImage drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
5 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
6 UIGraphicsEndImageContext();
7 return scaledImage;
8
9 UIImage *newImage = nil;
10 CGSize imageSize = sourceImage.size;
11 CGFloat width = imageSize.width;
12 CGFloat height = imageSize.height;
13 CGFloat targetWidth = targetSize.width;
14 CGFloat targetHeight = targetSize.height;
15 CGFloat scaleFactor = 0.0;
16 CGFloat scaledWidth = targetWidth;
17 CGFloat scaledHeight = targetHeight;
18 CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
19
20 UIGraphicsBeginImageContext(targetSize); // this will crop
21
22 CGRect thumbnailRect = CGRectZero;
23 thumbnailRect.origin = thumbnailPoint;
24 thumbnailRect.size.width = scaledWidth;
25 thumbnailRect.size.height = scaledHeight;
26
27 [sourceImage drawInRect:thumbnailRect];
28
29 newImage = UIGraphicsGetImageFromCurrentImageContext();
30 if(newImage == nil)
31 NSLog(@"could not scale image");
32
33 //pop the context to get back to the default
34 UIGraphicsEndImageContext();
35 return newImage;
36 }


 

//把图片按照新大小进行裁剪,生成一个新图片
 1 - (UIImage*) imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage *) sourceImage
2 {
3 // UIImage *sourceImage = self;
4 UIImage *newImage = nil;
5 CGSize imageSize = sourceImage.size;
6 CGFloat width = imageSize.width;
7 CGFloat height = imageSize.height;
8 CGFloat targetWidth = targetSize.width;
9 CGFloat targetHeight = targetSize.height;
10 CGFloat scaleFactor = 0.0;
11 CGFloat scaledWidth = targetWidth;
12 CGFloat scaledHeight = targetHeight;
13 CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
14
15 if (CGSizeEqualToSize(imageSize, targetSize) == NO)
16 {
17 CGFloat widthFactor = targetWidth / width;
18 CGFloat heightFactor = targetHeight / height;
19
20 if (widthFactor > heightFactor)
21 scaleFactor = widthFactor; // scale to fit height
22 else
23 scaleFactor = heightFactor; // scale to fit width
24 scaledWidth = width * scaleFactor;
25 scaledHeight = height * scaleFactor;
26
27 // center the image
28 if (widthFactor > heightFactor)
29 {
30 thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
31 }
32 else
33 if (widthFactor < heightFactor)
34 {
35 thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
36 }
37 }
38
39 UIGraphicsBeginImageContext(targetSize); // this will crop
40
41 CGRect thumbnailRect = CGRectZero;
42 thumbnailRect.origin = thumbnailPoint;
43 thumbnailRect.size.width = scaledWidth;
44 thumbnailRect.size.height = scaledHeight;
45
46 [sourceImage drawInRect:thumbnailRect];
47
48 newImage = UIGraphicsGetImageFromCurrentImageContext();
49 if(newImage == nil)
50 NSLog(@"could not scale image");
51
52 //pop the context to get back to the default
53 UIGraphicsEndImageContext();
54 return newImage;
55 }



 1 - (UIImage *)generatePhotoThumbnail:(UIImage *)image 
2 {
3 // Create a thumbnail version of the image for the event object.
4 CGSize size = image.size;
5 CGSize croppedSize;
6 CGFloat ratio = 64.0;//这个是设置转换后图片的尺寸大小
7 CGFloat offsetX = 0.0;
8 CGFloat offsetY = 0.0;
9
10 // check the size of the image, we want to make it
11 // a square with sides the size of the smallest dimension
12 if (size.width > size.height) {
13 offsetX = (size.height - size.width) / 2;
14 croppedSize = CGSizeMake(size.height, size.height);
15 } else {
16 offsetY = (size.width - size.height) / 2;
17 croppedSize = CGSizeMake(size.width, size.width);
18 }
19
20 // Crop the image before resize
21 CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
22   //裁剪图片
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
23     // Done cropping
24
25 // Resize the image
26 CGRect rect = CGRectMake(0.0, 0.0, ratio, ratio);
27
28 UIGraphicsBeginImageContext(rect.size);
29 [[UIImage imageWithCGImage:imageRef] drawInRect:rect];
30 UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
31 UIGraphicsEndImageContext();
32 // Done Resizing
33
34 return thumbnail;
35 }

实际应用简化
- (UIImage *)generatePhotoThumbnail:(UIImage *)image
{
CGRect rect=CGRectMake(0, 0, 60, 78);
  //裁剪图片
CGImageRef imageRef=CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, 0, 140, 182));

UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef]drawInRect:rect];
  //如果不裁剪图片可以直接画
    //[image drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
    UIImage *thumbnail=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return thumbnail;
}



附:

UIImage类并没有提供缩放图片需要用到的API,是不是觉得很吃惊?没关系,我们自己来添加一个。

 

定义缩放图片的Category

//  UIImage+Scale.h

@interface UIImage (scale)

-(UIImage*)scaleToSize:(CGSize)size;

@end

实现这个Category的定义



// UIImage+Scale.m  

#import "UIImage+Scale.h"

@implementation UIImage (scale)

-(UIImage*)scaleToSize:(CGSize)size
{// 创建一个bitmap的context// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);

// 绘制改变大小的图片
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];

// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

// 使当前的context出堆栈
UIGraphicsEndImageContext();

// 返回新的改变大小后的图片
return scaledImage;
}

@end



如何使用

// 创建图片
UIImage *image =[UIImage imageNamed:@"myImage.png"];

// 更改图片大小
UIImage *scaledImage =[image scaleToSize:CGSizeMake(25.0f, 35.0f)]


posted on 2012-02-16 22:38  pengyingh  阅读(2821)  评论(0编辑  收藏  举报

导航