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

This commit is contained in:
DreamPiggy 2017-10-26 23:51:26 +08:00
parent 4f7116afe9
commit ae67c97320
2 changed files with 36 additions and 1 deletions

View File

@ -14,6 +14,11 @@
#import <MobileCoreServices/MobileCoreServices.h> #import <MobileCoreServices/MobileCoreServices.h>
#endif #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) @implementation NSData (ImageContentType)
+ (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data { + (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data {
@ -48,7 +53,7 @@
if (data.length >= 12) { if (data.length >= 12) {
//....ftypheic //....ftypheic
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(4, 8)] encoding:NSASCIIStringEncoding]; 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; return SDImageFormatHEIC;
} }
} }
@ -73,6 +78,12 @@
case SDImageFormatTIFF: case SDImageFormatTIFF:
UTType = kUTTypeTIFF; UTType = kUTTypeTIFF;
break; break;
case SDImageFormatWebP:
UTType = kSDUTTypeWebP;
break;
case SDImageFormatHEIC:
UTType = kSDUTTypeHEIC;
break;
default: default:
// default is kUTTypePNG // default is kUTTypePNG
UTType = kUTTypePNG; UTType = kUTTypePNG;

View File

@ -397,6 +397,8 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
// Do not support WebP encoding // Do not support WebP encoding
case SDImageFormatWebP: case SDImageFormatWebP:
return NO; return NO;
case SDImageFormatHEIC:
return [[self class] canEncodeToHEICFormat];
default: default:
return YES; return YES;
} }
@ -469,6 +471,28 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
return YES; 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 #if SD_UIKIT || SD_WATCH
#pragma mark EXIF orientation tag converter #pragma mark EXIF orientation tag converter
+ (UIImageOrientation)sd_imageOrientationFromImageData:(nonnull NSData *)imageData { + (UIImageOrientation)sd_imageOrientationFromImageData:(nonnull NSData *)imageData {