简体   繁体   English

需要将.MP4文件从Documents文件夹保存到Photo Gallery中,但资产网址返回nil

[英]Need to save .MP4 file to Photo Gallery from documents folder but the asset url return nil

I have download the .mp4 file from sever to document directory and called the writeVideoAtPathToSavedPhotosAlbum method to save to gallery but its nor working.its throwing asset url nil.Can anyone plz help me with this issue 我已将.mp4文件从服务器下载到文档目录,并调用writeVideoAtPathToSavedPhotosAlbum方法保存到图库,但该方法无法正常工作。它抛出资产url无效。有人可以帮助我解决此问题

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

Check this below methods: 检查以下方法:

  -(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM