如何使用PHAsset从文件获取NSData [英] How to get NSData from file by using PHAsset

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

问题描述

我有文件路径

  file:///var/mobile/Media/DCIM/100APPLE/IMG_0197.mov 

但是当我尝试这段代码时 -

  NSError *错误; 
NSData * data = [NSData dataWithContentsOfFile:assetUrl.relativePath options:NSDataReadingMappedAlways error:& error];

我什么都没有,只是错误:


错误域= NSCocoaErrorDomain代码= 257不可用的
операцию。(Cocoa,ошибка257)UserInfo = 0x175a61380
{NSFilePath = / var / mobile / Media / DCIM / 100APPLE / IMG_0197.mov,
NSUnderlyingError = 0x17424e550不允许操作

文件存在,但我不能读取它。



但同时AVPlayer播放视频文件。



我试过

pre $ PHCetchResult * fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@ [assetUrl] options:nil];

但是我没有得到任何结果。

NSURL ,因为这些文件不在应用程序的沙盒中。某些框架(如 AVPlayer )具有例外授权,可以访问这些URL。
要访问 PHAsset 对象的数据,请查看 PHImageManager 的以下方法:



对于图片: requestImageDataForAsset



$ c> requestExportSessionForVideo


I have file at path

file:///var/mobile/Media/DCIM/100APPLE/IMG_0197.mov

But when I try this code-

NSError *error;
NSData *data = [NSData dataWithContentsOfFile:assetUrl.relativePath options:NSDataReadingMappedAlways error:&error];

I got nothing but error:

Error Domain=NSCocoaErrorDomain Code=257 "Не удалось завершить операцию. (Cocoa, ошибка 257)" UserInfo=0x175a61380 {NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0197.mov, NSUnderlyingError=0x17424e550 "Не удалось завершить операцию. Operation not permitted"}

File exists but I can't read it.

But at the same time AVPlayer normally plays video file.

I tried

PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[assetUrl] options:nil];

But I did not get any result.

解决方案

You can't access the NSURL directly as the files are outside of the sandbox of your app. Some frameworks (like AVPlayer) have exception entitlements and can access the URLs. To access the data of PHAsset objects take a look at the following methods of PHImageManager:

For images: requestImageDataForAsset

For videos: requestExportSessionForVideo

这篇关于如何使用PHAsset从文件获取NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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