我正在尝试从设备库的 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
然后您可以使用以下 method在 PHImageManager
中获取 PHAsset
的 NSData
:
- (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/