【问题标题】:iOS 5 - ALAssets library - converting a video's URL from assets to a NSDATA fileiOS 5 - ALAssets 库 - 将视频的 URL 从资产转换为 NSDATA 文件
【发布时间】:2012-05-10 19:04:31
【问题描述】:

所以我有一个应用程序可以让用户录制视频或从他们的照片库中挑选视频,然后将视频上传到服务器。录制视频后,我得到了资产的 URL。

我尝试使用以下代码将 NSData 设置为assetURL:

NSString *vidString =[NSString stringWithFormat:@"%@",savedVideoURL];
NSURL *vidURL = [NSURL URLWithString:vidString];
NSData *videoData = [NSData dataWithContentsOfURL:vidURL];

savedVideoURL 是录制过程中获取的资产 URL。资产 URL 如下所示:assets-library://asset/asset.MOV?id=6EB7A04D-3DF8-44E5-A6D8-FD98461AE75E&ext=MOV

当我尝试将 NSData 设置为该 url 时,之后 NSData 仍然等于 nil。

我无法使用以下解决方案:Get video NSData from ALAsset url iOS

还有其他人知道将 NSData 设置为该资产 URL 的方法吗?

提前致谢!

【问题讨论】:

    标签: video ios5 assets alassetslibrary


    【解决方案1】:

    别忘了导入:AssetsLibrary/ALAssetRepresentation.h

    和代码:

     ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
        [assetLibrary assetForURL:YOURURL resultBlock:^(ALAsset *asset) // substitute YOURURL with your url of video
        {
            ALAssetRepresentation *rep = [asset defaultRepresentation];
            Byte *buffer = (Byte*)malloc(rep.size);
            NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
            NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
            [data writeToFile:photoFile atomically:YES]; //you can remove this if only nsdata needed
        } 
        failureBlock:^(NSError *err) {
            NSLog(@"Error: %@",[err localizedDescription]);
        }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2021-12-02
      • 2017-01-03
      • 1970-01-01
      相关资源
      最近更新 更多