【问题标题】:Need to save .MP4 file to Photo Gallery from documents folder but the asset url return nil需要将 .MP4 文件从文档文件夹保存到照片库,但资产 url 返回 nil
【发布时间】:2016-02-09 06:21:02
【问题描述】:

我已将 .mp4 文件从服务器下载到文档目录并调用 writeVideoAtPathToSavedPhotosAlbum 方法保存到画廊,但它也不工作。它抛出资产 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");
        }
    }];

【问题讨论】:

  • 这一行的日志是什么:- NSLog(@"Moview URL:%@",movieURL);?在这里评论
  • 这一行有什么用:- NSURL *url = [movieURL copy];
  • url 为文档目录中视频位置的asset url
  • 您是否在变量“URL”中获得任何值,然后记录它?
  • URL 值在那里 file:///var/mobile/Containers/Data/Application/E0B3785E-D991-4FE3-A67A-C49A3D2DF032/Documents/test.mp4 但在 writeVideoAtPathToSavedPhotosAlbum 中,资产 url 是无。它也不会保存到画廊中

标签: ios objective-c media assets


【解决方案1】:

检查以下方法:

  -(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");


                  }];
             }
         }];
    }

【讨论】:

  • 我仍然将 Asset Url 设为 null。任何想法,可能是什么原因。 srcURL 正在返回值,但资产值为 null
  • 一旦检查文件路径,如果文件路径存在则发送到 saveTocamerRoll 方法。我已经做到了,对我来说效果很好。
  • 我已经尝试过路径存在。仍然没有保存到相册并且资产 url 为空
  • 资产显示为空且未保存到图库
  • ALAssetsLibrary 获取图像名称的替代方法是什么? ALAssetsLibrary 已弃用。我想获取被选中的图像的名称
【解决方案2】:

在 completionBlock 中尝试assetURL.absoluteString

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2012-04-18
    相关资源
    最近更新 更多