如何正式验证我的设备是否能够编码HEIC格式的图像?

9
我正在尝试使用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:],但我不想仅仅为此初始化和配置捕获会话。
是否有更直接的方法来查看硬件是否支持这种编码类型?
2个回答

8

Swift版本:

func supports(type: String) -> Bool {
    let supportedTypes = CGImageDestinationCopyTypeIdentifiers() as NSArray
    return supportedTypes.contains(type)
}

然后,只需使用您喜欢的类型(例如AVFileTypeHEIC)调用它:

if #available(iOS 11.0, *) {
    supports(type: AVFileTypeHEIC)
}

6

ImageIO有一个名为CGImageDestinationCopyTypeIdentifiers的函数,它返回一个CFArrayRef,其中包含了CGImageDestinationRef支持的类型。因此,可以使用以下代码来确定设备是否支持HEIC编码:

#import <AVFoundation/AVFoundation.h>
#import <ImageIO/ImageIO.h>

BOOL SupportsHEIC() {
  NSArray<NSString *> *types = CFBridgingRelease(
      CGImageDestinationCopyTypeIdentifiers());
  return [types containsObject:AVFileTypeHEIC];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接