Ios 需要将.MP4文件从documents文件夹保存到Photo Gallery,但资源url返回零
我已将.mp4文件从服务器下载到文档目录,并调用 writeVideoAtPathToSavedPhotosAlbum方法保存到gallery,但它不起作用。它抛出的资源url为零。有人能帮我解决这个问题吗Ios 需要将.MP4文件从documents文件夹保存到Photo Gallery,但资源url返回零,ios,objective-c,media,assets,Ios,Objective C,Media,Assets,我已将.mp4文件从服务器下载到文档目录,并调用 writeVideoAtPathToSavedPhotosAlbum方法保存到gallery,但它不起作用。它抛出的资源url为零。有人能帮我解决这个问题吗 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths ob
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此行的日志是什么:-NSLog(@“Moview URL:%@”,movieURL);?在这里注释这行有什么用:-NSURL*url=[movieURL copy];url是文档目录中视频位置的资源url。您是否在变量“url”中获取任何值,然后记录它?url值是否存在file:///var/mobile/Containers/Data/Application/E0B3785E-D991-4FE3-A67A-C49A3D2DF032/Documents/test.mp4 但在WriteVideoPathToSavedPhotosalbum中,资产url为零。它也没有保存到gallery。我仍然将资产url设置为空。任何想法,可能的原因是什么。srcURL返回值,但资产值为空检查文件路径后,如果文件路径存在,则发送到saveTocamerRoll方法。我已经这样做了,它对我来说工作正常。我已经尝试了路径存在。它仍然没有保存到相册,并且资产url为null。资产显示为null,并且它没有保存到Gallery。AlassetLibrary获取图像名称的替代方案是什么?不推荐使用该库。我想获取选中的图像的名称