我正在本地Documents中保存新创建的视频文件。然后,我想使用Photos框架将其复制到照片库中。
问题是,即使正在写出视频(并且我可以确认它并且可以查看),以下创建请求始终返回nil:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// NSString filePath points to the newly-created file in Documents directory
NSParameterAssert([[NSFileManager defaultManager] isReadableFileAtPath:filePath]);
// path looks fine
NSURL* url = [NSURL URLWithString:filePath];
PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
// got back nil from creationRequestForAssetFromVideoAtFileURL, so we will assert out here
NSParameterAssert(createAssetRequest);
}
completionHandler:^(BOOL success, NSError *error) {}];
答案 0 :(得分:0)
Not surprisingly, the URL was the problem. url = [NSURL URLWithString:filePath]
does not point to the movie file. It's fine if I get url
in (for example) the following way:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSArray *contents = [fileManager contentsOfDirectoryAtURL:documentsURL
includingPropertiesForKeys:@[]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lastPathComponent == 'movie.mov'"];
NSURL* url = nil;
for (NSURL *fileURL in [contents filteredArrayUsingPredicate:predicate]) {
url = fileURL;
}
答案 1 :(得分:0)
您需要使用NSURL* url = [NSURL fileURLWithPath:filePath];
。这是我的演示代码,对我很有用:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
NSURL *localFileUrl = [NSURL fileURLWithPath:localSavedPath];
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:localFileUrl];
if (localAssetCollection != nil) {
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:localAssetCollection];
[assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
}
} completionHandler:^(BOOL success, NSError *error) {
}];
我的演示也支持更改资产集合。