场景:项目中对react-native-syan-image-picker组件进行了修改,可以选择视频并且编辑
但选择图片时闪退,并报[NSURL initFileURLWithPath:]: nil string parameter错误
原因:因为选择的视频需要从iCloud上获取,在TZImageManager.m文件的
- (void)getVideoWithAsset:(PHAsset *)asset progressHandler:(void (^)(double progress, NSError *error, BOOL *stop, NSDictionary *info))progressHandler completion:(void (^)(AVPlayerItem *, NSDictionary *))completion;
方法里返回的info中拿不到视频地址,导致传给编辑视频界面的filePath是个nil,所以闪退
解决:
使用 requestExportSessionForVideo或者requestAVAssetForVideo代替requestPlayerItemForVideo方法即可
/// 从iCloud获取视频
- (void)getVideoWithAssetIniClound:(PHAsset *)asset completion:(void (^)(AVAssetExportSession * exportSession, NSDictionary * info))completion showLoading:(void (^)(BOOL *isShow))showLoading;
- (void)getVideoWithAssetIniClound:(PHAsset *)asset completion:(void (^)(AVAssetExportSession *, NSDictionary *))completion showLoading:(void (^)(BOOL))showLoading {
PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
options.networkAccessAllowed = YES;
options.progressHandler = ^(double progress, NSError * _Nullable error, BOOL * _Nonnull stop, NSDictionary * _Nullable info) {
if (progress < 1) showLoading(false);
};
[[PHImageManager defaultManager] requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetMediumQuality resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {
if (completion) completion(exportSession,info);
if (completion) showLoading(true);
}];
}
[self showLoading];
[[TZImageManager manager] getVideoWithAssetIniClound:model.asset completion:^(AVAssetExportSession *exportSession, NSDictionary *info) {
NSString* sandboxExtensionTokenKey = info[@"PHImageFileSandboxExtensionTokenKey"];
NSArray* arr = [sandboxExtensionTokenKey componentsSeparatedByString:@";"];
NSString* filePath = [arr.lastObject substringFromIndex:9];
if (filePath == nil) {
[tzImagePickerVc showAlertWithTitle:@"无法获取图片,请检查网络!"];
return;
}
UIVideoEditorController *editVC;
editVC = [[UIVideoEditorController alloc] init];
editVC.videoPath = filePath;
editVC.videoMaximumDuration = 60.0f;
editVC.delegate = self;
[self.navigationController presentViewController:editVC animated:YES completion:nil];
} showLoading:^(BOOL *isShow) {
if (!isShow) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicator startAnimating];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicator stopAnimating];
});
}
}];
/// 菊花loading
- (void)showLoading {
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize size = rect.size;
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, [self isNotchScreen] ? 64 : 88, size.width, size.height)];
[self.view addSubview:self.activityIndicator];
self.activityIndicator.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];
self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
}
/// 刘海判断
- (BOOL)isNotchScreen {
if (@available(iOS 11.0, *)) {
if (!UIEdgeInsetsEqualToEdgeInsets(self.view.safeAreaInsets, UIEdgeInsetsZero)) {
return YES;
}
}
return NO;
}
网友评论