快速服务器中的视频帖子

时间:2016-10-19 10:15:38

标签: ios objective-c iphone

我发布视频使用AFNetworking 3.0视频有限制180秒。有时我得到内存错误我想发送视频chunks.short视频发布在服务器上,但是当它很长我得到内存错误和应用程序崩溃

-(void)videoPost{
    [SVProgressHUD show];

    NSLog(@"DD Paths %@", Match_ID);
    NSString *urlString=@"http://202.164.59.107/stands_app/webservices/User/uploadfile";

    AFHTTPSessionManager *manager1 = [AFHTTPSessionManager manager];
    manager1.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    [manager1 POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
         NSString *documentsDirectory = [paths objectAtIndex:0];
         // NSLog(@"DD Paths %@", documentsDirectory);
         filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
         //NSLog(@"files array %@", filePathsArray);
         NSString *fullpath;
         for ( NSString *apath in filePathsArray )
         {
             fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
             videoURL =[NSURL fileURLWithPath:fullpath];
             [URLpaths addObject:videoURL];
         }
         // NSLog(@"vurl %@",vedioURL);
         //[URLpaths removeObjectAtIndex:0];
         videoURL=[URLpaths lastObject];
         NSData *videoData = [NSData dataWithContentsOfURL:videoURL];

         [formData appendPartWithFileData:videoData name:@"file" fileName:@"video.mov" mimeType:@"video/quicktime"];
         [formData appendPartWithFormData:[self.FileType dataUsingEncoding:NSUTF8StringEncoding]name:@"type"];
         [formData appendPartWithFormData:[LoginID dataUsingEncoding:NSUTF8StringEncoding]name:@"userid"];
         [formData appendPartWithFormData:[Match_ID dataUsingEncoding:NSUTF8StringEncoding]name:@"matchid"];
     } progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
         NSLog(@"Response: %@",task);
         NSLog(@"Response is success : %@", responseObject);

         NSString *Status=[responseObject valueForKey:@"success"];
         // success or not
         if([Status isEqualToString:@"1"])
         {

             [SVProgressHUD dismiss];
             [Utility showAlertWithMessage:@"Video Upload sucessfully."];
         }
         else
         {
             [Utility showAlertWithMessage:@"Faul to upload video."];
         }

     } failure:^(NSURLSessionDataTask *task, NSError *error) {
         //Error not task is sucess
         NSLog(@"Response data sucessfully : %@",task);
         [videoURL removeAllCachedResourceValues];
     }];
}

1 个答案:

答案 0 :(得分:1)

替换

  [formData appendPartWithFileData:videoData name:@"file" fileName:@"video.mov" mimeType:@"video/quicktime"];

  [formData appendPartWithFileURL:yourFileUrlForVideo name:@"file" fileName:@"video.mov" mimeType:@"video/quicktime" error:nil]; //I thing videoURL is FileURL in your case

并在下面注释,

  NSData *videoData = [NSData dataWithContentsOfURL:videoURL];

因为当您将任何内容转换为NSData时,它会使用您设备的内存,并且它会将整个文件一次转换为数据。因此,例如,如果您的视频大小为1000 MB,并将其转换为url到数据,那么它需要1000 MB的内存(内存意味着内存!)。所以最好直接从磁盘发送数据(存储设备 - 我的意思是文件目录),使用url而不将其转换为NSData。所以它不会使用不必要的内存。

您可以在应用程序运行时从memory usage开始关注应用程序的Debug Navigator