码迷,mamicode.com
首页 > 移动开发 > 详细

IOS图片缩放

时间:2015-07-31 12:27:31      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

1.自动缩放到指定大小

+ (UIImage *)thumbnailWithImage:(UIImage *)image size:(CGSize)asize

{

UIImage *newimage;


if (nil == image) {

newimage = nil;

}

else{

UIGraphicsBeginImageContext(asize);

[image drawInRect:CGRectMake(0, 0, asize.width, asize.height)];

newimage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

return newimage;

}

2.保持原来的长宽比,生成一个缩略图

+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize

{

UIImage *newimage;


if (nil == image) {

newimage = nil;

}

else{

CGSize oldsize = image.size;

CGRect rect;

if (asize.width/asize.height > oldsize.width/oldsize.height) {

rect.size.width = asize.height*oldsize.width/oldsize.height;

rect.size.height = asize.height;

rect.origin.x = (asize.width - rect.size.width)/2;

rect.origin.y = 0;

}

else{

rect.size.width = asize.width;

rect.size.height = asize.width*oldsize.height/oldsize.width;

rect.origin.x = 0;

rect.origin.y = (asize.height - rect.size.height)/2;

}


UIGraphicsBeginImageContext(asize);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);

UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background

[image drawInRect:rect];

newimage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}


return newimage;

}
技术分享

IOS图片缩放

标签:

原文地址:http://www.cnblogs.com/worldtraveler/p/4691633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!