多个视频上传进度

时间:2016-07-22 09:08:39

标签: ios nsurlsession nsurlsessionuploadtask

查看具有进度视图的多个视频上传示例。 在这里,我已经定义了步骤。

  1. 打开图库并选择视频。
  2. 从图库中选择视频,编辑完选择后,创建拇指图像。
  3. 在tableview或集合视图中使用拇指图像显示所有选定的视频
  4. 在Tableview或Collection视图中显示带有进度的视频上传过程。
  5. 任何人都知道如何做到这一点,我会对我有所帮助。

    可能我们可以使用NSUrlsession UPload任务但无法实现。

1 个答案:

答案 0 :(得分:1)

  1. 为此您可以使用MWPhotoBrowser
  2. 您可以使用以下方法生成所选视频的拇指图像。

    - (UIImage *)generateThumbImage : (NSString *)filepath {
          NSURL *url = [NSURL fileURLWithPath:filepath];
          AVAsset *asset = [AVAsset assetWithURL:url];
          AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
          imageGenerator.appliesPreferredTrackTransform = YES;
          CMTime time = [asset duration];
          time.value = 2;
          CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
          UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
          CGImageRelease(imageRef);  // CGImageRef won't be released by ARC
    
          return thumbnail;
    }
    
  3. 为此您可以查看" MWPhotoBrowser"并将生成的拇指图像传递给显示。
  4. 为此,您可以使用AFNetworking 3.0。并创建一个NSObject文件类来管理您的所有文件。创建具有imageView和progressView的collectionView。该collectionView类型是文件类型。

    @interface File : NSObject
    
    @property (nonatomic, strong) NSString *fullFilePath;
    @property (nonatomic) float overAllProgress;
    - (void)sendFile;
    
    @end
    
    
    @implementation File
    
    - (void)sendFile {
        NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST"            URLString:@"http://localhost/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    
         [formData appendPartWithFileURL:[NSURL fileURLWithPath:self.fullFilePath] name:@"photo_path" fileName:self.relativePath mimeType:@"video/quicktime" error:nil];
    
        } error:nil];
    
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
        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
              self.overAllProgress = uploadProgress.fractionCompleted;
              [[NSNotificationCenter defaultCenter] postNotificationName:@"imageprogress" object:self]
             });
          }
          completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            if (error) {
               NSLog(@"Error: %@", error);
            } else {
               NSLog(@"%@ %@", response, responseObject);
            }
          }];
    
          [uploadTask resume]; 
    
      @end
    
  5. 现在您需要处理文件进度通知。如下所示。

    -(void) viewWillAppear:(BOOL)animated{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileProgress:) name:@"imageprogress" object:nil];
    }
    
    - (void)viewDidUnload{
       [super viewDidUnload];
       // Release any retained subviews of the main view.
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"imageprogress" object:nil];
     }
    
    - (void)fileProgress:(NSNotification *)notif{
    
          File * info = [notif object];
          if([_arrFiles containsObject:info]){
              NSInteger row = [_arrFiles indexOfObject:info];
              NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:0];
              UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    
             [cell.progressView setProgress:info.overAllProgress animated:YES]
    
           }
    }