ios - 从 Assets 库 URL 获取 NSData

标签 ios objective-c

我正在尝试从设备库的 mp4 文件中获取 NSData。

这个链接看起来像这样:

assets-library://asset/asset.mp4?id=32515720-939A-456F-958F-0B2F397416EB&ext=mp4

我试过这段代码:

ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc((NSUInteger)rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:(NSUInteger)rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

但是 defaultRepresentation 在 iOS 9 中被弃用了。

我也尝试过 [NSData dataWithContentsOfFile:url]; 但它返回 nil。

最佳答案

ALAsset 的整个类在 iOS 9 中已弃用。您现在应该考虑改用 PHAsset。有一个 API 可以从旧 Assets url ALAsset 获取 PHAsset。查看following :

The Assets Library framework is deprecated in iOS 8.0 and later, replaced by the Photos framework. Use this method if your app has previously stored URLs from ALAsset objects and you need to retrieve the corresponding Photos framework objects.

+ (PHFetchResult<PHAsset *> *)fetchAssetsWithALAssetURLs:(NSArray<NSURL *> *)assetURLs
                                                 options:(PHFetchOptions *)options

然后您可以使用以下 methodPHImageManager 中获取 PHAssetNSData:

- (PHImageRequestID)requestImageDataForAsset:(PHAsset *)asset
                                     options:(PHImageRequestOptions *)options
                               resultHandler:(void (^)(NSData *imageData,
                                                       NSString *dataUTI,
                                                       UIImageOrientation orientation,
                                                       NSDictionary *info))resultHandler

关于ios - 从 Assets 库 URL 获取 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33871575/

相关文章:

ios - 使用当前位置更新屏幕中心

ios - 即使我们不像其他 ViewController 生命周期方法一样在 viewcontroller 中重写 loadView ,它也会被调用吗?

ios - 似乎无法隐藏我的 UIImageView

objective-c - 将 UITableView 和数据源/委托(delegate)放在单独的类中不起作用 (XCode 4.2)

ios - SIGABRT错误-恰好在第三次运行之后(iPhone和iPad模拟器)

ios - 在 Bitbucket 存储库上推送具有多个项目的工作区

objective-c - 如何在 ipad/iPhone 中为现有的照片应用程序添加自定义菜单项

ios - 在 XCode 6.2 中下载 iOS 7.1 模拟器进行离线安装

ios - 如何在WatchKit中获取 watch 电池

ios - 单例 NSMutableDictionary 属性不允许 setObject :forKey