gpt4 book ai didi

ios - 从 Assets 库 URL 获取 NSData

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:07 24 4
gpt4 key购买 nike

我正在尝试从设备库的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com