2017-08-27 52 views
3

我正在尝试使用ImageIO以HEIC文件格式保存图像。该代码看起来是这样的:验证我的设备是否能够以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的设备,但无法对旧设备,并在模拟器上(也可根据苹果),由于未能初始化destination和错误消息findWriterForType:140: unsupported file format 'public.heic'。我找不到任何直接测试硬件是否支持HEIC而无需初始化新映像目标和测试可空性的方法。

有基于AVFoundation的API用于检查照片是否可以使用HEIC保存,例如使用-[AVCapturePhotoOutput supportedPhotoCodecTypesForFileType:],但我不想为此初始化和配置捕获会话。

是否有更直接的方法来查看硬件是否支持这种编码类型?

回答

4

ImageIO具有一个称为CGImageDestinationCopyTypeIdentifiers的函数,它返回CGImageDestinationRef的受支持类型的CFArrayRef。因此,下面的代码可以被用来确定HEIC编码是否支持在设备上:

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

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

夫特版本:

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

然后只需用您的优选类型称之为(例如AVFileTypeHEIC):

if #available(iOS 11.0, *) { 
    supports(type: AVFileTypeHEIC) 
} 
相关问题