最近需要用到图片压缩,本来苹果是有一个图片压缩的方法的,但是函数只能说不是很符合我们现在的需求,尤其是一张几M的图片想要压缩成40K用苹果的API这是不可能做到的,我在网络上查找了很多资料,都没有相关现成的第三方库或方法给我用,后来我绝望了,只能自己写一个了。
以下是我设计这个压缩图片的思路----
(1)先根据原图跟需要压缩成多少KB算出压缩的比例。
(2)获取原图的Size,根据算出的比例来计算出缩小后的图片Size
(3)再通过判断原图的宽跟高的比例,通过反推,计算出压缩后小图的宽跟高
(4)重新渲染图片,再回调出来
以下是代码部分
Swift 代码部分
// 图片回调代理
typealias ImageBlock = (image:UIImage) -> Void
/**
压缩图片
- parameter image: 需要压缩的图片
- parameter imageBytes: 压缩的大小
- parameter block: 压缩后的回调
*/
func compressedImageFiles(image:UIImage?, imageBytes:CGFloat = 30, block:ImageBlock){
var imageCope = image
let fBytes = imageBytes * 1024
var uploadImageData:NSData?
uploadImageData = UIImagePNGRepresentation(imageCope!)
let imageSize = imageCope!.size
let imageWidth = imageSize.width
let imageHeight = imageSize.height
if CGFloat((uploadImageData?.length)!) > fBytes {
dispatch_async(dispatch_queue_create("CompressedImage", DISPATCH_QUEUE_SERIAL)) { () -> Void in
let fBytes = fBytes/CGFloat((uploadImageData?.length)!)
let fScaleArea = imageWidth * imageHeight * fBytes
var fImageScale:CGFloat = 0.0
var dHeight:CGFloat? = 0.0
var dWidth:CGFloat? = 0.0
if imageWidth > imageHeight {
fImageScale = imageWidth/imageHeight
dHeight = CGFloat(sqrt(Double(fScaleArea/fImageScale)))
dWidth = dHeight! * fImageScale
}
else if imageHeight > imageWidth {
fImageScale = imageHeight/imageWidth
dWidth = CGFloat(sqrt(Double(fScaleArea/fImageScale)))
dHeight = dWidth! * fImageScale
}
else
{
dWidth = CGFloat(sqrt(Double(fScaleArea)))
dHeight = dWidth!
}
UIGraphicsBeginImageContext(CGSizeMake(dWidth!, dHeight!))
imageCope!.drawInRect(CGRectMake(0, 0, dWidth!, dHeight!))
imageCope = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
uploadImageData = UIImagePNGRepresentation(imageCope!)
if CGFloat((uploadImageData?.length)!) > fBytes {
uploadImageData = UIImageJPEGRepresentation(imageCope!, 1);
}
print("图片已经压缩成\(uploadImageData!.length/1024)KB")
imageCope = UIImage(data: uploadImageData!)!
dispatch_sync(dispatch_get_main_queue(), { () -> Void in
block(image: imageCope!)
})
}
}
else
{
block(image: imageCope!)
}
}
Swift代码调用上面函数
/**
* 这句函数调用的意思是,将 1.jpg 这张图片压缩成 100KB 左右的大小
*/
self.compressedImageFiles(UIImage(named: "1.jpg"), imageBytes: 100) { (image) in
print("这个是压缩后的图片\(image)")
}
下面部分是用OC代码部分
OC 代码
/**
* 压缩图片
*
* @param image 需要压缩的图片
* @param fImageBytes 希望压缩后的大小(以KB为单位)
*
* @return 压缩后的图片
*/
- (void)compressedImageFiles:(UIImage *)image
imageBytes:(CGFloat)fImageBytes
imageBlock:(imageBlock)block
{
__block UIImage *imageCope = image;
fImageBytes = fImageBytes * 1024;
__block NSData *uploadImageData = nil;
uploadImageData = UIImagePNGRepresentation(imageCope);
CGSize size = imageCope.size;
CGFloat imageWidth = size.width;
CGFloat imageHeight = size.height;
if (uploadImageData.length > fImageBytes) {
dispatch_async(dispatch_queue_create("CompressedImage", DISPATCH_QUEUE_SERIAL), ^{
CGFloat fBytes = fImageBytes/uploadImageData.length;
CGFloat fScaleArea = imageWidth * imageHeight * fBytes;
CGFloat fImageScale = 0.0;
CGFloat dHeight = 0.0;
CGFloat dWidth = 0.0;
if (imageWidth > imageHeight) {
fImageScale = imageWidth/imageHeight;
dHeight = sqrtf(fScaleArea/fImageScale);
dWidth = dHeight * fImageScale;
}
else if (imageHeight > imageWidth) {
fImageScale = imageHeight/imageWidth;
dWidth = sqrtf(fScaleArea/fImageScale);
dHeight = dWidth * fImageScale;
}
else {
dWidth = sqrtf(fScaleArea);
dHeight = dWidth;
}
UIGraphicsBeginImageContext(CGSizeMake(dWidth, dHeight));
[imageCope drawInRect:CGRectMake(0, 0, dWidth, dHeight)];
imageCope = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
uploadImageData = UIImagePNGRepresentation(imageCope);
if (uploadImageData.length > fImageBytes) {
uploadImageData = UIImageJPEGRepresentation(imageCope, 1);
}
NSLog(@"图片已经压缩成 %luKB",uploadImageData.length/1024);
imageCope = [[UIImage alloc] initWithData:uploadImageData];
dispatch_sync(dispatch_get_main_queue(), ^{
block(imageCope);
});
});
}
else
{
block(imageCope);
}
}
OC调用代码
/**
* 这句函数调用的意思是,将 1.jpg 这张图片压缩成 100KB 左右的大小
*/
[self compressedImageFiles:[UIImage imageNamed:@"1.jpg"] imageBytes:100 imageBlock:^(UIImage *image) {
NSLog(@"这个是压缩后的图片%@",image);
}];
希望大家不吝赐教。。。
代码已经修改,删除不人性化的地方~!
转载需要标注原文地址~!