ios - NSData dataWithContentsOfURL 用于相机胶卷中的 URL

标签 ios objective-c nsdata nsurl photolibrary

我有一些存储在相机胶卷中的视频的 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 来 self 的本地应用程序文档文件夹,我可以毫无问题地生成 NSData。我想知道是否不允许从相机胶卷中存储的视频创建 NSData 。我想知道我是否需要使用照片框架或其他方法才能获得许可。

最佳答案

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

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 有一些额外的权限信息,而我之前通过 [NSURL fileURLWithPath] 的方法没有这些信息。

关于ios - NSData dataWithContentsOfURL 用于相机胶卷中的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44466746/

相关文章:

ios - 错误 : Indexing expression is invalid because subscript type 'NSUInteger' is not an integral or objective c pointer type

ios - 将 UIImage 转换为 NSData 并在 Swift 中转换回 UIImage?

ios - 如何在 swiftUI 中呈现 crossDissolve View ?

ios - NSNotificationCenter是否需要取消UILocalnotficiation?

ios - 防止 NSString 的堆检查

objective-c - NSApplicationPresentationHideDock 没有效果

objective-c - C 风格的 block 会导致内存泄漏吗?

ios - 在 iOS 11 上从右到左显示大标题

ios - 不使用 UIImagePngrepresentation 或 UIImageJpegRepresentation 将 UIImage 转换为 NSData

swift - 代码为-1100 的 NSURLErrorDomain