2019独角兽企业重金招聘Python工程师标准>>>
一上传:
- (void)uploadFileWithfileList:(NSMutableArray *)params
Option:(NSDictionary *)optiondic
Inferface:(NSString *)requestURL
FileType:(NSString *)type
MimeType:(NSString *)mimeType
Success:(void(^)(id responseObject))successBlock
Failure:(void(^)(NSError *error))failBlock
progress:(void (^)(float progress))progress{
AFHTTPRequestSerializer *afRequest = [AFHTTPRequestSerializer serializer];
NSMutableURLRequest *request = [afRequest multipartFormRequestWithMethod:@"POST" URLString:requestURL parameters:optiondic constructingBodyWithBlock:^(id
//将图片装换为二进制格式--UIImageJPEGRepresentation第一个参数为要上传的图片,第二个参数是图片压缩的倍数
//如果要上传多张图片把下面两句代码放到for循环里即可
for (int i = 0; i if ([params[i] isKindOfClass:[UIImage class]]) { UIImage *image = params[i] ; NSData *imageData =UIImageJPEGRepresentation(image, 0.3); [formData appendPartWithFileData:imageData name:@"files" fileName:[NSString stringWithFormat:@"%d.%@",i,type] mimeType:mimeType]; }else if ([params[i] isKindOfClass:[NSString class]]) { NSURL *url = [NSURL fileURLWithPath:params[i]]; NSData *data = [NSData dataWithContentsOfURL:url]; [formData appendPartWithFileData:data name:@"files" fileName:[NSString stringWithFormat:@"%d.%@",i,type] mimeType:mimeType]; }else if([params[i] isKindOfClass:[NSURL class]]) { NSData *data = [NSData dataWithContentsOfURL:params[i]]; [formData appendPartWithFileData:data name:@"files" fileName:[NSString stringWithFormat:@"%d.%@",i,type] mimeType:mimeType]; } } } error:nil]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; //manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //manager.requestSerializer = [AFJSONRequestSerializer serializer]; NSURLSessionUploadTask *uploadTask; uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) { // This is not called back on the main queue. // You are responsible for dispatching to the main queue for UI updates dispatch_async(dispatch_get_main_queue(), ^{ //Update the progress view //进度 progress(uploadProgress.fractionCompleted); }); } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { if (error) { failBlock(error); } else { successBlock(responseObject); } }]; [uploadTask resume]; } 二,视屏压缩 #import #import /** * 压缩成功Block * * @param resultPath 返回压缩成功的视频路径 */ typedef void (^CompressionSuccessBlock)(NSString *resultPath,float memorySize); // 定义成功的Block 函数 @interface CompressionVideoTool : NSObject /** * method Comperssion Video 压缩视频的方法, 该方法将压缩过的视频保存到沙河文件, 如果压缩过的视频不需要再进行保留, 可调用 removeCompressedVideoFromDocuments 方法, 将其删除即可 * * @param url SourceVideoURL 被压缩视频的URL * @param compressionType 压缩可选类型 AVAssetExportPresetLowQuality AVAssetExportPresetMediumQuality AVAssetExportPresetHighestQuality AVAssetExportPreset640x480 AVAssetExportPreset960x540 AVAssetExportPreset1280x720 AVAssetExportPreset1920x1080 AVAssetExportPreset3840x2160 * * @return 返回压缩后的视频路径 */ + (void)compressedVideoOtherMethodWithURL:(NSURL *)url compressionType:(NSString *)compressionType compressionResultPath:(CompressionSuccessBlock)resultPathBlock; /** * 获取视频的大小 * * @return 返回视频的大小 float 类型 */ + (float)countVideoTotalMemorySizeWithURL:(NSURL *)url; /** * 清楚沙盒文件中, 压缩后的视频所有 */ + (void)removeCompressedVideoFromDocuments; @end #import "CompressionVideoTool.h" #import #define CompressionVideoPaht [NSHomeDirectory() stringByAppendingFormat:@"/Documents/CompressionVideoField"] @interface CompressionVideoTool () @end @implementation CompressionVideoTool + (void)compressedVideoOtherMethodWithURL:(NSURL *)url compressionType:(NSString *)compressionType compressionResultPath:(CompressionSuccessBlock)resultPathBlock { NSString *resultPath; // NSData *data = [NSData dataWithContentsOfURL:url]; // CGFloat totalSize = (float)data.length / 1024 / 1024; AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil]; NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; // 所支持的压缩格式中是否有 所选的压缩格式 if ([compatiblePresets containsObject:compressionType]) { AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:compressionType]; NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可 [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"]; NSFileManager *manager = [NSFileManager defaultManager]; BOOL isExists = [manager fileExistsAtPath:CompressionVideoPaht]; if (!isExists) { [manager createDirectoryAtPath:CompressionVideoPaht withIntermediateDirectories:YES attributes:nil error:nil]; } resultPath = [CompressionVideoPaht stringByAppendingPathComponent:[NSString stringWithFormat:@"outputJFVideo-%@.mov", [formater stringFromDate:[NSDate date]]]]; NSLog(@"压缩文件路径 resultPath = %@",resultPath); exportSession.outputURL = [NSURL fileURLWithPath:resultPath]; exportSession.outputFileType = AVFileTypeMPEG4; exportSession.shouldOptimizeForNetworkUse = YES; [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { if (exportSession.status == AVAssetExportSessionStatusCompleted) { NSData *data = [NSData dataWithContentsOfFile:resultPath]; float memorySize = (float)data.length / 1024 / 1024; NSLog(@"视频压缩后大小 %f", memorySize); resultPathBlock (resultPath, memorySize); } else { NSLog(@"压缩失败"); } }]; } else { // JFLog(@"不支持 %@ 格式的压缩", compressionType); } } + (float)countVideoTotalMemorySizeWithURL:(NSURL *)url { NSData *data = [NSData dataWithContentsOfURL:url]; CGFloat totalSize = (float)data.length / 1024 / 1024; return totalSize; } + (void)removeCompressedVideoFromDocuments { NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:CompressionVideoPaht]) { [[NSFileManager defaultManager] removeItemAtPath:CompressionVideoPaht error:nil]; } } @end