需要将 .MP4 文件从文档文件夹保存到照片库,但资产 url return 无
Need to save .MP4 file to Photo Gallery from documents folder but the asset url return nil
我已经从服务器下载了.mp4文件到文档目录并调用了
writeVideoAtPathToSavedPhotosAlbum 方法保存到图库,但它也 working.its 抛出资产 url nil.Can 任何人请帮助我解决这个问题
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");
}];
}
}];
}
在 completionBlock
中尝试 assetURL.absoluteString
我已经从服务器下载了.mp4文件到文档目录并调用了 writeVideoAtPathToSavedPhotosAlbum 方法保存到图库,但它也 working.its 抛出资产 url nil.Can 任何人请帮助我解决这个问题
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");
}];
}
}];
}
在 completionBlock
中尝试 assetURL.absoluteString