I use the following codes to upload video to a server, which requires me to convert video from video format to NSData . However, when the video is large (for example, a 10-minute video), the application crashes due to memory pressure. How can i solve this?
- (void)uploadVideo { NSDictionary *params = nil; NSString *NSURLSessionIdentifier = [NSString stringWithFormat:@"%@%@",@"my.bundle.identifier.",[self getTimeString]]; NSURLSessionConfiguration *sessionConfig; // SessionConfiguration With iOS Version if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:NSURLSessionIdentifier]; } else { sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:NSURLSessionIdentifier]; } sessionConfig.HTTPMaximumConnectionsPerHost = 1; NSURLSession *uploadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue new]]; OMGMultipartFormData *multipartFormData = [OMGMultipartFormData new]; NSString *url = @"SOME_UPLOAD_URL"; // ========= PROBLEMATIC LINE below ========= self.video_data = [NSData dataWithContentsOfURL:self.video_url]; // ========= PROBLEMATIC LINE above ========= [multipartFormData addFile:self.video_data parameterName:@"file" filename:@"file.mp4" contentType:@"video/mp4"]; NSURLRequest *rq = [OMGHTTPURLRQ POST:url:multipartFormData]; id path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"upload.NSData"]; [rq.HTTPBody writeToFile:path atomically:YES]; [[uploadSession uploadTaskWithRequest:rq fromFile:[NSURL fileURLWithPath:path]] resume]; }
ps self.video_url is the URL of the file specified by UIImagePickerController , which filters only the video to be selected. Then I select a 10 minute video.
ps I got AFNetworking in the same application, can it help with background AFNetworking ?
ios uiimagepickercontroller nsdata nsurlsession
Raptor
source share