this code works and saves the downloaded file to camera roll if the file is an image but it doesn't save it when it's a video file .
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSData * data = [NSData dataWithContentsOfURL:location];
UIImage * image = [UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
UISaveVideoAtPathToSavedPhotosAlbum(location.path, self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
dispatch_async(dispatch_get_main_queue(), ^{
[self.ProgressView setHidden:NO];
});
}
when I get the path of downloaded file it is a temp file like this:
/private/var/mobile/Containers/Data/Application/DE97D98D-760F-48DC-AFBB-C61C5E3185A0/tmp/CFNetworkDownload_5DOCCe.tmp
and I think since UISaveVideoAtPathToSavedPhotosAlbum wants target video's path to save the reason it doesn't work is that it cannot access the video file inside .tmp
please help! it's been quite a while I'm struggling with this.
I found my solution:
NSData * data = [NSData dataWithContentsOfURL:location];
[[NSFileManager defaultManager] moveItemAtURL:location toURL:[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] error:nil];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * FilePath = [documentsDirectory stringByAppendingPathComponent:jsonTitle];
[data writeToFile:FilePath atomically:YES];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(FilePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(FilePath, nil, nil, nil);
} else {
NSLog(@"error");
}
NSFileManager * removeDownloadAfterSave;
[removeDownloadAfterSave removeItemAtPath:FilePath error:nil];