废话不多说,直接上代码
/**
@param image 传入图片
@param asize 需求的大小
@return 缩略图
+(UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize
{
UIImage *newimage;
if (nil == image) {
nil;
else{
CGSize oldsize = image.size;
CGRect rect;
if (asize.width/asize.height > oldsize.width/oldsize.height) {
size.width = asize.height*oldsize.width/oldsize.height;
size.height = asize.height;
origin.x = (asize.width - rect.size.width)/2;
origin.y = 0;
else{
size.width = asize.width;
size.height = asize.width*oldsize.height/oldsize.width;
origin.x = 0;
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
drawInRect:rect];
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimage;
}