来自相机胶卷的 NSData dataWithContentsOfURL for URL

NSData dataWithContentsOfURL for URL from camera roll

我有一些 URL 用于存储在相机胶卷中的视频。例如:

文件:///var/mobile/Media/DCIM/100APPLE/IMG_0249.MP4

我想将这些视频进一步处理为 NSData,但是如果我使用

NSURL* videoURL = [NSURL fileURLWithPath:@"file:///var/mobile/Media/DCIM/100APPLE/IMG_0249.MP4"];
NSData *imageData = [NSData dataWithContentsOfURL:videoURL options:NSDataReadingMappedIfSafe error:&error];

数据始终为零。我收到以下错误:

Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0249.MP4” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0249.MP4, NSUnderlyingError=0x165351d0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

我注意到如果 URL 来自我的本地应用程序文档文件夹,我可以毫无问题地生成 NSData。我想知道是否不允许从相机胶卷中存储的视频中创建 NSData。我想知道我是否需要使用照片框架或其他方法才能获得权限。

事实证明,我无法将相机胶卷视频的 URL 直接转换为 NSData,我需要使用 PHImageManager 来做到这一点(并使用我在过去):

PHFetchResult* assets = [PHAsset fetchAssetsWithLocalIdentifiers:@[videoID] options:nil];

PHAsset *asset = assets.firstObject;
if (asset.mediaType == PHAssetMediaTypeVideo) {
    [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
        if ([asset isKindOfClass:[AVURLAsset class]]) {
            AVURLAsset* urlAsset = (AVURLAsset*)asset;
            NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL];
            [self sendToTwitter:data withMessage:message account: account];
        }
    }];
}

我的猜测是 AVURLAsset* urlAsset 有一些额外的权限信息,我之前通过 [NS[=19= 的方法] 文件URLWithPath] 没有.