我可以加载普通图像: public.image类型。
苹果proRaw(土坯原始图像类型: DNG格式)可以在iPhone 12系列中使用。
因此,我用原始图像捕获,并希望从应用程序中加载DNG文件。
但是我不能使用PHPicker加载图像。通常,下面的代码。
PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] init];
configuration.filter = [PHPickerFilter anyFilterMatchingSubfilters:@[[PHPickerFilter imagesFilter], [PHPickerFilter livePhotosFilter]]];
PHPickerViewController *pickerController = [[PHPickerViewController alloc] initWithConfiguration:configuration];
pickerController.delegate = self;
[pickerController setModalPresentationStyle:UIModalPresentationCustom];
[pickerController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[viewController presentViewController:pickerController animated:YES completion:nil];
-(void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results API_AVAILABLE(ios(14)) {
[picker dismissViewControllerAnimated:YES completion:nil];
PHPickerResult *result = [results firstObject];
if ([result.itemProvider canLoadObjectOfClass:[UIImage class]]) { // 1
[result.itemProvider loadObjectOfClass:[NSObject class] completionHandler:^(__kindof id<NSItemProviderReading> _Nullable object, NSError * _Nullable error) {
if ([object isKindOfClass:[UIImage class]]) {
UIImage *image = object;
...
}
}];
}在注释1行中,返回否。
如何使用PHPicker加载原始图像?
发布于 2021-09-15 03:41:23
在我看来,使用loadFileRepresentation将照片数据放到CGImage对象中是可行的。类似于:
result.itemProvider.loadFileRepresentation(forTypeIdentifier: "public.image") { url, _ in
guard let url = url,
let data = NSData(contentsOf: url),
let source = CGImageSourceCreateWithData(data, nil),
let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil) else {
// handle
}
let image = UIImage(cgImage)
...
}或
[result.itemProvider loadFileRepresentationForTypeIdentifier:@"public.image" completionHandler:^(NSURL * _Nullable url, NSError * _Nullable error) {
if (url) {
NSData *data = [NSData dataWithContentsOfURL:url];
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);
UIImage *image = [UIImage imageWithCGImage:cgImage];
...
}
}];您可能需要使用CGImageSourceCopyPropertiesAtIndex获得正确的方向以获取元数据字典,使用kCGImagePropertyOrientation键找到正确的值,将其从CGImagePropertyOrientation转换为UIImage.Orientation,并将其传递给UIImage初始化器。
它比仅仅使用loadObjectOfClass要复杂一些,但它不需要照片访问授权。
https://stackoverflow.com/questions/65896083
复制相似问题