Ios 访问.tmp中的视频
如果下载的文件是图像文件,则此代码可以工作并将其保存到camera roll中,但如果文件是视频文件,则不保存Ios 访问.tmp中的视频,ios,objective-c,nsurlsessiondownloadtask,Ios,Objective C,Nsurlsessiondownloadtask,如果下载的文件是图像文件,则此代码可以工作并将其保存到camera roll中,但如果文件是视频文件,则不保存 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSData * data = [NSData dataWithContentsOf
- (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];
});
}
当我得到下载文件的路径时,它是一个临时文件,如下所示:
/私有/var/mobile/Containers/Data/Application/dev7d98d-760F-48DC-AFBB-C61C5E3185A0/tmp/CFNetworkDownload_5DOCCe.tmp
我认为,由于UISaveVideoAtPathToSavedPhotosAlbum希望目标视频的路径保存,因此它无法工作的原因是它无法访问.tmp中的视频文件
请帮忙!我已经花了很长时间来解决这个问题。尝试使用ALAssestsLibrary @属性(强,非原子)库*库
NSURL *videoURL = [NSURL URLWithString:videoURLString];
dispatch_async(dispatch_get_main_queue(), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:videoURL];
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:videoName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"downloadComplete! is %f", (float)totalBytesRead / totalBytesExpectedToRead);
}];
[operation setCompletionBlock:^{
NSLog(@"downloadComplete!");
[self.library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:filePath] completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"%@", error.description);
}
else {
NSLog(@"Done :)");
NSString *fileDownloadStatus = [NSString stringWithFormat:@"Downloading of %@ Finished", videoName];
[SVProgressHUD showImage:[UIImage imageNamed:@"Mp4_Icon"] status:fileDownloadStatus];
}
}];
}];
[operation start];
});
我找到了我的解决方案:
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];
看看这个:谢谢,检查并更新了问题。谢谢,现在我可以访问视频,它有mp4格式,但UIVideoAtPathPath与SavedPhotosAlbum兼容。我认为它与相机卷不兼容。它与相机卷兼容