iOS - 视频压缩和图片压缩

**第一、视频压缩 **

一、输出地址

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可
    [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
    //一般.mp4
    NSURL *newVideoUrl  = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]] ;//这个是保存在app自己的沙盒路径里,后面可以选择是否在上传后删除掉。我建议删除掉,免得占空间。
     [self convertVideoQuailtyWithInputURL:_videoUrl outputURL:newVideoUrl completeHandler:nil];
}

二、判断状态

/* http://blog.csdn.net/wzios/article/details/52100697 */
- (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:@"%f s", [MSUCameraPhotoVc getVideoDuration:outputURL]]);
                 NSLog(@"%@", [NSString stringWithFormat:@"%.2f kb", [MSUCameraPhotoVc getFileSize:[outputURL path]]]);
                 self.videoPathUrl = outputURL;
                 //UISaveVideoAtPathToSavedPhotosAlbum([outputURL path], self, nil, NULL);//这个是保存到手机相册
                 
//                 [self alertUploadVideo:outputURL];
                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"AVAssetExportSessionStatusFailed");
                 break;
         }
         
     }];
}

第二、图片压缩

    /* 压缩图片到指定尺寸  */
    + (UIImage *)compressOriginalImage:(UIImage *)image toSize:(CGSize)size{
        UIImage * resultImage = image;
        UIGraphicsBeginImageContext(size);
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        UIGraphicsEndImageContext();
        return image;
    }
    
    /* 压缩图片到指定大小  */
    + (NSData *)compressOriginalImage:(UIImage *)image withScale:(NSInteger)scale{
        NSData *data = nil;
        data =UIImageJPEGRepresentation(image,scale);
    
    //    if(!UIImagePNGRepresentation(image)) {
    //        data =UIImageJPEGRepresentation(image,scale);
    //    }else{
    //        data =UIImagePNGRepresentation(image);
    //    }
        return data;
    }

你可能感兴趣的:(iOS - 视频压缩和图片压缩)