A,//传入的参数:1、生成图片的大小 2、压缩比 3、存放图片的路径
+ (void)createThumbImage:(UIImage
}
{
}
//压缩图片
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
}
cell.typeImageView.imageURL = [NSURL URLWithString:@"http://qq.5068.com/uploads/allimg/130107/111Qa446-13.jpg"];
通过打印NSData压缩前后的长度来看是否压缩.
这段“许靖昕”先生分享的代码将示范如何缩小 UIImage
- @implementation
UIImage (Extras) - #pragma
mark - - #pragma
mark Scale and crop image - -
(UIImage*)imageByScalingAndCroppin gForSize:(CGSize)targetSize - {
- UIImage
*sourceImage = self; - UIImage
*newImage = nil; - CGSize
imageSize = sourceImage.size; - CGFloat
width = imageSize.width; - CGFloat
height = imageSize.height; - CGFloat
targetWidth = targetSize.width; - CGFloat
targetHeight = targetSize.height; - CGFloat
scaleFactor = 0.0; - CGFloat
scaledWidth = targetWidth; - CGFloat
scaledHeight = targetHeight; - CGPoint
thumbnailPoint = CGPointMake(0.0,0.0); - if
(CGSizeEqualToSize(imageSize, targetSize) == NO) -
{ -
CGFloat widthFactor = targetWidth / width; -
CGFloat heightFactor = targetHeight / height; -
if (widthFactor > heightFactor) -
scaleFactor = widthFactor; // scale to fit height -
else -
scaleFactor = heightFactor; // scale to fit width -
scaledWidth = width * scaleFactor; -
scaledHeight = height * scaleFactor; -
// center the image -
if (widthFactor > heightFactor) -
{ -
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; -
} -
else -
if (widthFactor < heightFactor) -
{ -
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; -
} -
} - UIGraphicsBeginImageCont
ext(targetSize); // this will crop - CGRect
thumbnailRect = CGRectZero; - thumbnailRect.origin
= thumbnailPoint; - thumbnailRect.size.width
= scaledWidth; - thumbnailRect.size.height
= scaledHeight; - [sourceImage
drawInRect:thumbnailRect]; - newImage
= UIGraphicsGetImageFromCu rrentImageContext(); - if(newImage
== nil) -
NSLog(@"could not scale image"); - //pop
the context to get back to the default - UIGraphicsEndImageContex
t(); - return
newImage; - }