最近需要用到圖片壓縮逻澳,本來蘋果是有一個圖片壓縮的方法的躏筏,但是函數(shù)只能說不是很符合我們現(xiàn)在的需求,尤其是一張幾M的圖片想要壓縮成40K用蘋果的API這是不可能做到的涡相,我在網(wǎng)絡上查找了很多資料,都沒有相關現(xiàn)成的第三方庫或方法給我用剩蟀,后來我絕望了催蝗,只能自己寫一個了。
以下是我設計這個壓縮圖片的思路----
(1)先根據(jù)原圖跟需要壓縮成多少KB算出壓縮的比例育特。
(2)獲取原圖的Size丙号,根據(jù)算出的比例來計算出縮小后的圖片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("圖片已經(jīng)壓縮成\(uploadImageData!.length/1024)KB")
imageCope = UIImage(data: uploadImageData!)!? ? ? ? ? ? dispatch_sync(dispatch_get_main_queue(), { () -> Void in
block(image: imageCope!)
})
}
}? ? else
{
block(image: imageCope!)
}
}
Swift代碼調用上面函數(shù)
/**
*? 這句函數(shù)調用的意思是,將 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(@"圖片已經(jīng)壓縮成 %luKB",uploadImageData.length/1024);
imageCope = [[UIImage alloc] initWithData:uploadImageData];? ? ? ? ? ? dispatch_sync(dispatch_get_main_queue(), ^{
block(imageCope);
});
});
}? ? else
{
block(imageCope);
}
}
OC調用代碼
/**
*? 這句函數(shù)調用的意思是棉浸,將 1.jpg 這張圖片壓縮成 100KB 左右的大小
*/[self compressedImageFiles:[UIImage imageNamed:@"1.jpg"] imageBytes:100 imageBlock:^(UIImage *image) {? ? NSLog(@"這個是壓縮后的圖片%@",image);
}];