从assets-library URL获取NSData [英] Get NSData from assets-library URL

查看:480
本文介绍了从assets-library URL获取NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从设备库中获取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];

但iOS 9中不推荐使用 defaultRepresentation / p>

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

解决方案

在iOS 9中不推荐使用 ALAsset 的整个类。您现在应该考虑使用 PHAsset 。有一个API可以从旧资产网址 ALAsset 获取 PHAsset 。请参阅以下


资产库框架在iOS 8.0及更高版本中已弃用,取而代之的是照片框架。如果您的应用程序先前已存储来自ALAsset对象的URL,并且您需要检索相应的Photos框架对象,请使用此方法。




  +(PHFetchResult< PHAsset *> *)fetchAssetsWithALAssetURLs:(NSArray< NSURL *> *)assetURLs 
选项:(PHFetchOptions *)期权

然后你可以使用以下中的rel =nofollow>方法 PHImageManager 获取 PHAsset NSData

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


I'm trying to get NSData from mp4 file from device library.

This link look like this:

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

I've tried this code:

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];

But defaultRepresentation deprecated in iOS 9.

I've also tried [NSData dataWithContentsOfFile:url]; but it returns nil.

解决方案

The entire class of of ALAsset is deprecated in iOS 9. You should now consider using PHAsset instead. There is a API to get the PHAsset from the old asset url ALAsset. See the 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

And then you could use the following method in PHImageManager to get the NSData of the PHAsset:

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

这篇关于从assets-library URL获取NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆