Reputation: 49
I am posting video using AFNetworking 3.0 video have limit 180 seconds.Some time i am getting memory error i want to send video in chunks.short video is posted on server but when it is long i am getting memory error and app crash.
-(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];
}];
}
Upvotes: 1
Views: 47
Reputation: 27448
Replace your
[formData appendPartWithFileData:videoData name:@"file" fileName:@"video.mov" mimeType:@"video/quicktime"];
with
[formData appendPartWithFileURL:yourFileUrlForVideo name:@"file" fileName:@"video.mov" mimeType:@"video/quicktime" error:nil]; //I thing videoURL is FileURL in your case
and comment below line,
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
Because when you convert anything to NSData
then it's use memory of your device and it's convert whole file in to data at a time. so for example if your video's size is 1000 mb and you convert it to url to data then it's require 1000 mb of memory(memory means ram!). so it's better to send data directly from disk (storage of device - i mean documentsdirectory) by using url
without converting it's to NSData
. So it do not use unnecessary memory.
You can take watch on your app's memory usage
from Debug Navigator
while your application is running.
Upvotes: 1