通过 NSURLSession 上传大视频导致内存压力崩溃 [英] Upload large video via NSURLSession causes memory pressure crash
问题描述
我使用以下代码将视频上传到服务器,这需要我将视频从视频格式转换为NSData
.但是,当视频较大时(例如 10 分钟的视频),App 会因内存压力而崩溃.我该如何解决这个问题?
I use the following codes to upload video to server, which requires me to convert the video from video format to NSData
. However, when the video is large (e.g. 10 minute video), the App crashes due to memory pressure. How can I resolve 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];
}
附言self.video_url
是 UIImagePickerController
给出的文件 URL,它只过滤要选择的视频.然后我选择一个 10 分钟的视频.
p.s. self.video_url
is a file URL given by UIImagePickerController
which filters only video to choose. I then choose a 10-minute video.
附言我在同一个应用程序中也有 AFNetworking
,它可以帮助后台传输吗?
p.s. I got AFNetworking
in same App too, can it help with background transfer?
推荐答案
您应该能够通过使用 NSMutableURLRequest 并利用其 setHTTPBodyStream setter.
You should be able to this by using a NSMutableURLRequest and utilizing its setHTTPBodyStream setter.
以下是改编自我的一些代码的片段.它处理超过 10 分钟的视频方式.大部分是 60 到 90 分钟的大型视频.
The following are snippets adapted from some code of mine. It handled well for video way over 10 mins. mostly large videos of 60 - 90 minutes.
NSData *movieData = [NSData dataWithContentsOfFile:theMovieSourceString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"video/quicktime" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"attachment; filename=\"%@\"",yourMovieSourceString] forHTTPHeaderField:@"Content-Disposition"];
[request setValue:[NSString stringWithFormat:@"%ld",(unsigned long)[movieData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBodyStream:[NSInputStream inputStreamWithFileAtPath:yourMovieSourceString]];
您现在可以将此请求与您的 NSURLConnection
You can now use this request with your NSURLConnection
NSURLConnection *connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
这篇关于通过 NSURLSession 上传大视频导致内存压力崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!