
Paste_Image.png
前言
很多時(shí)候我們需要把一些圖片、音頻、視頻上傳到服務(wù)器,于是就有了文件壓縮這個(gè)問題了,這篇文章就小結(jié)一下具體的文件壓縮實(shí)現(xiàn)吧。
圖片壓縮
我們都知道蘋果手機(jī)拍照后的照片格式的是JPG(全稱是 JPEG),手機(jī)截圖的格式是PNG的
**壓:是指文件體積變小,但是像素?cái)?shù)不變,長寬尺寸不變,那么質(zhì)量可能下降。**
在Iphone上有兩種讀取圖片數(shù)據(jù)的簡(jiǎn)單方法
**UIImageJPEGRepresentation和UIImagePNGRepresentation.**
UIImageJPEGRepresentation
函數(shù)需要兩個(gè)參數(shù):圖片的引用和壓縮系數(shù)而UIImagePNGRepresentation
只需要圖片引用作為參數(shù).
UIImagePNGRepresentation(UIImage \*image)
要比UIImageJPEGRepresentation(UIImage* image, 1.0)
返回的圖片數(shù)據(jù)量大很多.
同樣的一張照片, 使用UIImagePNGRepresentation(image)
返回的數(shù)據(jù)量大小為199K
而 UIImageJPEGRepresentation(image, 1.0)返回的數(shù)據(jù)量大小只為140K,比前者少了59K
如果對(duì)圖片的清晰度要求不是極高,建議使用UIImageJPEGRepresentation
,可以大幅度降低圖片數(shù)據(jù)量.比如,剛才拍攝的圖片,通過調(diào)用
UIImageJPEGRepresentation(image, 1.0)讀取數(shù)據(jù)時(shí),返回的數(shù)據(jù)大小為140K
,但更改壓縮系數(shù)為0.5再讀取數(shù)據(jù)時(shí),返回的數(shù)據(jù)大小只有11K
#兩者差了14倍
,大大壓縮了圖片的數(shù)據(jù)量,而且清晰度并沒有相差多少,圖片的質(zhì)量并沒有明顯的降低。因此,
在讀取圖片數(shù)據(jù)內(nèi)容時(shí),建議優(yōu)先使用UIImageJPEGRepresentation,
并可根據(jù)自己的實(shí)際使用場(chǎng)景,設(shè)置壓縮系數(shù),進(jìn)一步降低圖片數(shù)據(jù)量大小。
CGFloat dividend = 1024.0 * 1024.0;
//得到圖片的data
NSData *data = [self imageConversationDataWith:upImage];
//判斷圖片所占內(nèi)存的大小
CGFloat memory = data.length / dividend;
// 循環(huán)壓縮圖片, 直到滿足要求
while (memory > 2.0) {
upImage = [self scaleToSize:upImage scale:0.9];
data = [self imageConversationDataWith:upImage];
memory = data.length / dividend;
}
# 內(nèi)存壓
- (NSData *)imageConversationDataWith:(UIImage *)image {
NSData *data;
if (UIImagePNGRepresentation(image)) {
// png圖像。
data = UIImagePNGRepresentation(image);
}else {
// JPEG圖像。
data = UIImageJPEGRepresentation(image, 1);
}
return data;
}
#縮 是指文件的尺寸變小,也就是像素?cái)?shù)減少,而長寬尺寸變小,文件體積同樣會(huì)減小。
- (UIImage *)scaleToSize:(UIImage *)image scale:(CGFloat)scale{
// 創(chuàng)建一個(gè)bitmap的context
CGSize size = CGSizeMake(image.size.width * scale, image.size.height * scale);
// 并把它設(shè)置成為當(dāng)前正在使用的context
UIGraphicsBeginImageContext(size);
// 繪制改變大小的圖片
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 從當(dāng)前context中創(chuàng)建一個(gè)改變大小后的圖片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使當(dāng)前的context出堆棧
UIGraphicsEndImageContext();
// 返回新的改變大小后的圖片
return scaledImage;
}
音頻壓縮:
以10s為例: wav格式 167k

Snip20161127_1.png
可以看的出 相同時(shí)長 wav格式的內(nèi)存是最大的,mp3編碼格式的文件占用的內(nèi)存是最小的,10倍的差別
使用libmp3lame 進(jìn)行MP3編碼格式的轉(zhuǎn)碼

Snip20161207_2.png
#include "lame.h"
//經(jīng)過轉(zhuǎn)化,音頻文件的大小得到了很大的壓縮
//這里的轉(zhuǎn)化,隱藏了兩個(gè)參數(shù) :原始路徑cafPathStr 輸出路徑 mp3PathStr
#pragma mark - caf轉(zhuǎn)mp3
- (void)audio_PCMtoMP3
{
@try {
int read, write;
FILE *pcm = fopen([cafPathStr cStringUsingEncoding:1], "rb"); //source 被轉(zhuǎn)換的音頻文件位置
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3PathStr cStringUsingEncoding:1], "wb"); //output 輸出生成的Mp3文件位置
const int PCM_SIZE = 8192;
const int MP3_SIZE = 8192;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];
lame_t lame = lame_init();
lame_set_in_samplerate(lame, 11025.0);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);
do {
read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
if (read == 0)
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
else
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
fwrite(mp3_buffer, write, 1, mp3);
} while (read != 0);
lame_close(lame);
fclose(mp3);
fclose(pcm);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
}
@finally {
NSLog(@"MP3生成成功: %@",mp3PathStr);
}
}
*******************************************************
//計(jì)算文件大小
long long fileSize = [self fileSizeAtPath:mp3PathStr]/1024.0;
//單個(gè)文件的大小
- (long long) fileSizeAtPath:(NSString*)filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}else{
NSLog(@"計(jì)算文件大?。何募淮嬖?);
}
return 0;
}
視頻壓縮
以2s為例
未經(jīng)壓縮的視頻大小有 8968k 而經(jīng)過壓縮的視頻長度有 240k,倆者差了 40多倍
# iOS錄制的視頻是mov格式的,安卓和PC不支持,因此要轉(zhuǎn)換成MP4,并且要壓縮。
# inputURL原始文件路徑、 outputURL 壓縮后文件的路徑
- (void) convertVideoQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
completeHandler:(void (^)(AVAssetExportSession*))handler
{
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
// NSLog(resultPath);
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse= YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
switch (exportSession.status) {
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"AVAssetExportSessionStatusCompleted");
NSLog(@"%@",[NSString stringWithFormat:@"33 %f s", [self getVideoLength:outputURL]]);
NSLog(@"%@", [NSString stringWithFormat:@"44 %.2f kb", [self getFileSize:[outputURL path]]]);
//UISaveVideoAtPathToSavedPhotosAlbum([outputURL path], self, nil, NULL);//這個(gè)是保存到手機(jī)相冊(cè)
vidoePathUrL = outputURL;
// [self alertUploadVideo:outputURL];
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed");
break;
}
}];
}
小結(jié)
通過上文的分析,圖片壓縮后比原文件小了 14倍,音頻文件比之前小了10多倍,而視頻文件比之前小了40多倍,OMG,由此可以看出,文件上傳中的壓縮工作是多么的必要??!