我正在尝试使用ImageIO以HEIC文件格式保存图像.代码看起来像这样:
NSMutableData *imageData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData(
    (__bridge CFMutableDataRef)imageData,
    (__bridge CFStringRef)AVFileTypeHEIC, 1, NULL);
if (!destination) {
  NSLog(@"Image destination is nil");
  return;
}
// image is a CGImageRef to compress.
CGImageDestinationAddImage(destination, image, NULL);
BOOL success = CGImageDestinationFinalize(destination);
if (!success) {
  NSLog(@"Failed writing the image");
  return;
}
这适用于具有A10的设备,但在旧设备和模拟器(也是根据Apple)上失败,因为未能初始化destination和错误消息findWriterForType:140: unsupported file format 'public.heic'.我找不到任何直接的方法来测试硬件是否支持HEIC而不初始化新的图像目的地并测试可空性.
有基于AVFoundation的API用于检查是否可以使用HEIC保存照片,例如使用-[AVCapturePhotoOutput supportedPhotoCodecTypesForFileType:],但我不想为此初始化和配置捕获会话.
有没有更直接的方法来查看硬件是否支持这种编码类型?
ImageIO的有一个调用的函数CGImageDestinationCopyTypeIdentifiers返回一个CFArrayRef支持的类型的CGImageDestinationRef。因此,可以使用以下代码来确定设备是否支持HEIC编码:
#import <AVFoundation/AVFoundation.h>
#import <ImageIO/ImageIO.h>
BOOL SupportsHEIC() {
  NSArray<NSString *> *types = CFBridgingRelease(
      CGImageDestinationCopyTypeIdentifiers());
  return [types containsObject:AVFileTypeHEIC];
}
迅捷版:
func supports(type: String) -> Bool {
    let supportedTypes = CGImageDestinationCopyTypeIdentifiers() as NSArray
    return supportedTypes.contains(type)
}
然后用你喜欢的类型(例如AVFileTypeHEIC)调用它:
if #available(iOS 11.0, *) {
    supports(type: AVFileTypeHEIC)
}
| 归档时间: | 
 | 
| 查看次数: | 880 次 | 
| 最近记录: |