IOS 录制视频,压缩和上传

1.录制或选择本地视频

    1.录制视频
    - (void)startvideo  
    {  
       UIImagePickerController *ipc = [[UIImagePickerController alloc] init];  
        ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
        NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie"  
        ipc.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];//设置媒体类型为public.movie  
        [self presentViewController:ipc animated:YES completion:nil];  
        ipc.videoMaximumDuration = 30.0f;//30秒  
        ipc.delegate = self;//设置委托  
     } 

    2.获取本地的视频
    - (void)choosevideo  
    {  
        UIImagePickerController *ipc = [[UIImagePickerController alloc] init];  
        ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//sourcetype有三种分别是camera,photoLibrary和photoAlbum  
        NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie"  
        ipc.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];//设置媒体类型为public.movie  
        [self presentViewController:ipc animated:YES completion:nil];  
        ipc.delegate = self;//设置委托  
     } 

    3.获取视频的时长和大小
    - (CGFloat) getFileSize:(NSString *)path  
    {  
      NSLog(@"%@",path);  
      NSFileManager *fileManager = [NSFileManager defaultManager];  
      float filesize = -1.0;  
      if ([fileManager fileExistsAtPath:path]) {  
       NSDictionary *fileDic = [fileManager attributesOfItemAtPath:path error:nil];
    unsigned long long size = [[fileDic objectForKey:NSFileSize] longLongValue];  
    filesize = 1.0*size/1024;  
    }else{  
    NSLog(@"找不到文件");  
    }  
    return filesize;  
    }//此方法可以获取文件的大小,返回的是单位是KB。  
      - (CGFloat) getVideoLength:(NSURL *)URL  
     {  
         AVURLAsset *avUrl = [AVURLAsset assetWithURL:URL];  
         CMTime time = [avUrl duration];  
         int second = ceil(time.value/time.timescale);  
         return second;  
     }//此方法可以获取视频文件的时长。 

2.接收并压压缩

   1. 接收并压缩
  -(void)alertUploadVideo:(NSURL*)URL{  
CGFloat size = [self getFileSize:[URL path]];  
NSString *message;  
NSString *sizeString;  
CGFloat sizemb= size/1024;  
if(size<=1024){  
    sizeString = [NSString stringWithFormat:@"%.2fKB",size];  
}else{  
    sizeString = [NSString stringWithFormat:@"%.2fMB",sizemb];  
}  
  
  
  
  
if(sizemb<2){  
    [self uploadVideo:URL];  
}  
  
else if(sizemb<=5){  
    message = [NSString stringWithFormat:@"视频%@,大于2MB会有点慢,确定上传吗?", sizeString];  
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil  
                                                                              message: message  
                                                                       preferredStyle:UIAlertControllerStyleAlert];  
      
      
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {  
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshwebpages" object:nil userInfo:nil];  
        [[NSFileManager defaultManager] removeItemAtPath:[URL path] error:nil];//取消之后就删除,以免占用手机硬盘空间(沙盒)  
          
    }]];  
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {  
        [self uploadVideo:URL]; 
    }]];  
    [self presentViewController:alertController animated:YES completion:nil];  

      
}else if(sizemb>5){  
    message = [NSString stringWithFormat:@"视频%@,超过5MB,不能上传,抱歉。", sizeString];  
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil  
                                                                              message: message  
                                                                       preferredStyle:UIAlertControllerStyleAlert];  
      
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {  
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshwebpages" object:nil userInfo:nil];  
        [[NSFileManager defaultManager] removeItemAtPath:[URL path] error:nil];//取消之后就删除,以免占用手机硬盘空间  
          
    }]];  
    [self presentViewController:alertController animated:YES completion:nil];  
} 

}

3.上传视频

  1. -(void)uploadVideo:(NSURL*)URL{  
      //[MyTools showTipsWithNoDisappear:nil message:@"正在上传..."];  
      NSData *data = [NSData dataWithContentsOfURL:URL];  
      MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"www.ylhuakai.com" customHeaderFields:nil];  
      NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];  
      NSString *updateURL;  
      updateURL = @"/alflower/Data/sendupdate";  
      [dic setValue:[NSString stringWithFormat:@"%@",User_id] forKey:@"openid"];  
     [dic setValue:[NSString stringWithFormat:@"%@",[self.web objectForKey:@"web_id"]] forKey:@"web_id"];  
      [dic setValue:[NSString stringWithFormat:@"%i",insertnumber] forKey:@"number"];  
      [dic setValue:[NSString stringWithFormat:@"%i",insertType] forKey:@"type"];  
  
      MKNetworkOperation *op = [engine operationWithPath:updateURL params:dic httpMethod:@"POST"];  
      [op addData:data forKey:@"video" mimeType:@"video/mpeg" fileName:@"aa.mp4"];  
     [op addCompletionHandler:^(MKNetworkOperation *operation) {  
    NSLog(@"[operation responseData]-->>%@", [operation responseString]);  
    NSData *data = [operation responseData];  
    NSDictionary *resweiboDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];  
    NSString *status = [[resweiboDict objectForKey:@"status"]stringValue];  
    NSLog(@"addfriendlist status is %@", status);  
    NSString *info = [resweiboDict objectForKey:@"info"];  
    NSLog(@"addfriendlist info is %@", info);  
   // [MyTools showTipsWithView:nil message:info];  
    // [SVProgressHUD showErrorWithStatus:info];  
    if ([status isEqualToString:@"1"])  
    {  
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshwebpages" object:nil userInfo:nil];  
        [[NSFileManager defaultManager] removeItemAtPath:[URL path] error:nil];//上传之后就删除,以免占用手机硬盘空间;  
          
    }else  
    {  
        //[SVProgressHUD showErrorWithStatus:dic[@"info"]];  
    }  
    // [[NSNotificationCenter defaultCenter] postNotificationName:@"StoryData" object:nil userInfo:nil];  
      
      
}errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {  
    NSLog(@"MKNetwork request error : %@", [err localizedDescription]);  
}];  
    [engine enqueueOperation:op];  
}

你可能感兴趣的:(IOS 录制视频,压缩和上传)