Authored by DreamPiggy

Provide a way to detect HEIC encoding compatibility. So that we can avoid encoding to this format.

... ... @@ -14,6 +14,11 @@
#import <MobileCoreServices/MobileCoreServices.h>
#endif
// Currently Image/IO does not support WebP
#define kSDUTTypeWebP ((__bridge CFStringRef)@"public.webp")
// AVFileTypeHEIC is defined in AVFoundation via iOS 11, we use this without import AVFoundation
#define kSDUTTypeHEIC ((__bridge CFStringRef)@"public.heic")
@implementation NSData (ImageContentType)
+ (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data {
... ... @@ -48,7 +53,7 @@
if (data.length >= 12) {
//....ftypheic
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(4, 8)] encoding:NSASCIIStringEncoding];
if ([testString hasPrefix:@"ftyp"] && [testString hasSuffix:@"heic"]) {
if ([testString isEqualToString:@"ftypheic"]) {
return SDImageFormatHEIC;
}
}
... ... @@ -73,6 +78,12 @@
case SDImageFormatTIFF:
UTType = kUTTypeTIFF;
break;
case SDImageFormatWebP:
UTType = kSDUTTypeWebP;
break;
case SDImageFormatHEIC:
UTType = kSDUTTypeHEIC;
break;
default:
// default is kUTTypePNG
UTType = kUTTypePNG;
... ...
... ... @@ -397,6 +397,8 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
// Do not support WebP encoding
case SDImageFormatWebP:
return NO;
case SDImageFormatHEIC:
return [[self class] canEncodeToHEICFormat];
default:
return YES;
}
... ... @@ -469,6 +471,28 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
return YES;
}
+ (BOOL)canEncodeToHEICFormat
{
static BOOL canEncode = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSMutableData *imageData = [NSMutableData data];
CFStringRef imageUTType = [NSData sd_UTTypeFromSDImageFormat:SDImageFormatHEIC];
// Create an image destination.
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, imageUTType, 1, NULL);
if (!imageDestination) {
// Can encode to HEIC
canEncode = NO;
} else {
// Can't encode to HEIF
CFRelease(imageDestination);
canEncode = YES;
}
});
return canEncode;
}
#if SD_UIKIT || SD_WATCH
#pragma mark EXIF orientation tag converter
+ (UIImageOrientation)sd_imageOrientationFromImageData:(nonnull NSData *)imageData {
... ...