我已将 .mp4 文件从服务器下载到文档目录并调用
writeVideoAtPathToSavedPhotosAlbum 方法保存到画廊,但它也不工作。它抛出 Assets url nil。任何人都可以帮助我解决这个问题
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:path];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
NSLog(@"Moview URL:%@",movieURL);
NSURL *url = [movieURL copy];
[library writeVideoAtPathToSavedPhotosAlbum:url
completionBlock:^(NSURL *assetURL, NSError *error)
{
NSLog(@"Asset Url:%@",assetURL);
if(!error) {
NSLog(@"\t ! Error");
NSLog(@"\t Error: %@", [error localizedDescription]);
NSLog(@"\t Error code %d", [error code]);
}
if(error != nil) {
NSLog(@"\t ERROR != NIL");
NSLog(@"\t Error - Image Failed To Save With Error: %@", [error localizedDescription]);
NSLog(@"\t Error code %d", [error code]);
}
if(error == nil) {
NSLog(@"\t ERROR == NIL");
}
}];
最佳答案
检查以下方法:
-(void)ButtonAction:(id)sender
{
NSURL *video = [NSURL URLWithString:YOURURL];
NSData *data = [NSData dataWithContentsOfURL:video];
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/myvideo_%@.mp4",docDirPath,CURRENT_TIMESTAMP];
[data writeToFile:filePath atomically:YES];
NSURL *urlPath = [[NSURL alloc] initFileURLWithPath:filePath];
[self saveToCameraRoll:urlPath dict:(NSDictionary *)sender];
}
-(void) saveToCameraRoll:(NSURL *)srcURL dict:(NSDictionary *)dictValues
{
NSLog(@"srcURL: %@", srcURL);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:srcURL completionBlock:^(NSURL *assetURL, NSError *error)
{
NSURL *savedAssetURL = assetURL;
NSLog(@"asset url %@",assetURL);
if(error)
{
error handling
}
else
{
[library assetForURL:savedAssetURL resultBlock:^(ALAsset * alAsset)
{
NSLog(@"Saved");
}failureBlock:^(NSError *error)
{
NSLog(@"Not Saved");
}];
}
}];
}
关于ios - 需要将 .MP4 文件从文档文件夹保存到照片库,但 Assets url 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35285224/