亲测:两种方式都有效
第一种方式:缩略成固定的尺寸大小
- (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize { UIImage *newimage; if (nil == image) { newimage = nil; } else { UIGraphicsBeginImageContext(asize); [image drawInRect:CGRectMake(, , asize.width, asize.height)]; newimage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return newimage; }
第二种方式:保持原来图片的宽高比
- (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)/; rect.origin.y = ; } else{ rect.size.width = asize.width; rect.size.height = asize.width*oldsize.height/oldsize.width; rect.origin.x = ; rect.origin.y = (asize.height - rect.size.height)/; } UIGraphicsBeginImageContext(asize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); UIRectFill(CGRectMake(, , asize.width, asize.height));//clear background [image drawInRect:rect]; newimage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return newimage; }