我正在使用 GMImagePicker 来获取相机胶卷的 Assets 。 选择单个或多个视频后,我在 arrayArray 中获得了 PHAsset。 此格式中的 assetArray 数据
"<PHAsset: 0x7fe0e8f21130> 40D4733D-4C7B-443D-8093-C28E39ACA45E/L0/001 mediaType=2/0, sourceType=1, (720x480), creationDate=2016-02-14 09:50:58 +0000, location=0, hidden=0, favorite=0 "
如何将此视频存储到我的应用程序文档目录中? 我用来存储视频的代码是
for (PHAsset *asset in assetArray) {
if (asset.mediaType==PHAssetMediaTypeVideo) {
NSLog(@"Video found");
}
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
NSURL *URL = [(AVURLAsset *)asset URL];
NSMutableData *responseData=[[NSMutableData alloc] init];
responseData = [NSMutableData dataWithContentsOfURL:URL];
if (responseData.length>0) {
NSString * lastPath = [URL.absoluteString lastPathComponent];//videoname.mp4
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@",docDirPath,lastPath];
[responseData writeToFile:filePath atomically:YES];
}
}];
}
我还检查了保存视频的文件路径。视频可用,但出现此错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
请帮我解决这个问题。
最佳答案
根据我的说法,使用 NSMutableData 而不是 NSData 它可以接受 Nullable 值
self.responseData = [NSMutableData dataWithContentsOfURL:location];
if (self.responseData.length>0) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString * lastPath = [url.absoluteString lastPathComponent];//videoname.mp4
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@",docDirPath,lastPath];
[self.responseData writeToFile:filePath atomically:YES];
}
});
希望对你有帮助。
关于ios - 如何将多个视频保存到应用程序文档目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35908915/