访问 .tmp 中的视频

accessing the video inside .tmp

如果文件是图像,此代码可以工作并将下载的文件保存到相机胶卷,但如果是视频文件则不会保存。

    - (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];
    });

}

当我得到下载文件的路径时,它是一个像这样的临时文件:

/private/var/mobile/Containers/Data/Application/DE97D98D-760F-48DC-AFBB-C61C5E3185A0/tmp/CFNetworkDownload_5DOCCe.tmp

而且我认为因为 UISaveVideoAtPathToSavedPhotosAlbum 想要目标视频的路径来保存它不起作用的原因是它无法访问 .tmp 中的视频文件

请帮忙!我已经为此苦苦挣扎了一段时间。

尝试使用 ALAssestsLibrary

@属性(强,非原子)ALAssetsLibrary *library;

    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];