/*
* 保持原来的长宽比,生成一个缩略图
* &-image 待传入UIImage
* &-size 待传入UIImage要改变图像的尺寸
* 返回处理好的UIImage
* 保持原来的长宽比,生成一个缩略图
* &-image 待传入UIImage
* &-size 待传入UIImage要改变图像的尺寸
* 返回处理好的UIImage
*/
+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)siz/** * 保持原来的长宽比,生成一个缩略图 */
+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)size
{
UIImage *newimage;
if (nil == image) {
newimage = nil;
}
else{
CGSize oldsize = image.size;
CGRect rect;
if (size.width/size.height > oldsize.width/oldsize.height) {
rect.size.width = size.height*oldsize.width/oldsize.height;
rect.size.height = size.height;
rect.origin.x = (size.width - rect.size.width)/2;
rect.origin.y = 0;
}
else{
rect.size.width = size.width;
rect.size.height = size.width*oldsize.height/oldsize.width;
rect.origin.x = 0;
rect.origin.y = (size.height - rect.size.height)/2;
}
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, size.width, size.height));//clear background
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
{
UIImage *newimage;
if (nil == image) {
newimage = nil;
}
else{
CGSize oldsize = image.size;
CGRect rect;
if (size.width/size.height > oldsize.width/oldsize.height) {
rect.size.width = size.height*oldsize.width/oldsize.height;
rect.size.height = size.height;
rect.origin.x = (size.width - rect.size.width)/2;
rect.origin.y = 0;
}
else{
rect.size.width = size.width;
rect.size.height = size.width*oldsize.height/oldsize.width;
rect.origin.x = 0;
rect.origin.y = (size.height - rect.size.height)/2;
}
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, size.width, size.height));//clear background
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}