Skip to content

Commit ba8b93f

Browse files
committed
feat: support backport to Xcode 15 SDK
1 parent 9f766bc commit ba8b93f

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ You can use those directly, or create similar components of your own, by using t
122122
- watchOS 2.0 or later
123123
- macOS 10.11 or later (10.15 for Catalyst)
124124
- visionOS 1.0 or later
125-
- Xcode 14.0 or later (visionOS requires Xcode 15.0)
125+
- Xcode 15.0 or later
126126

127127
#### Backwards compatibility
128128

SDWebImage/Core/SDImageCoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeToHDR;
9999
#pragma mark - Image Encoding Options
100100
/**
101101
A NSUInteger value (NSNumber) to provide converting to HDR during encoding. Read the below carefully to choose the value.
102-
@note 0 means SDR; 1 means ISO HDR (at least using 10 bits per components or above, supported by AVIF/HEIF/JPEG-XL); 2 means ISO Gain Map Image (may use 8 bits per components, supported AVIF/HEIF/JPEG-XL, as well as traditional JPEG)
102+
@note 0 means SDR; 1 means ISO HDR (at least using 10 bits per components or above, supported by AVIF/HEIF/JPEG-XL); 2 means ISO Gain Map HDR (may use 8 bits per components, supported by AVIF/HEIF/JPEG-XL, as well as traditional JPEG)
103103
@note Gain Map like a mask image with metadata, which contains the depth/bright information for each pixel (or smaller, 1/4 resolution), which used to convert between HDR and SDR.
104104
@note If you use CIImage as HDR pipeline, you can export as CGImage for encoding. (But it's also recommanded to use CIImage's `JPEGRepresentationOfImage` or `HEIFRepresentationOfImage`)
105105
@note Supported by iOS 18 and above when using ImageIO coder (third-party coder can support lower firmware)

SDWebImage/Core/SDImageIOAnimatedCoder.m

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828

2929
// Specify File Size for lossy format encoding, like JPEG
3030
static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestinationRequestedFileSize";
31+
// Support Xcode 15 SDK, use raw value instead of symbol
32+
static NSString * kSDCGImageDestinationEncodeRequest = @"kCGImageDestinationEncodeRequest";
33+
static NSString * kSDCGImageDestinationEncodeToSDR = @"kCGImageDestinationEncodeToSDR";
34+
static NSString * kSDCGImageDestinationEncodeToISOHDR = @"kCGImageDestinationEncodeToISOHDR";
35+
static NSString * kSDCGImageDestinationEncodeToISOGainmap = @"kCGImageDestinationEncodeToISOGainmap";
36+
3137

3238
// This strip the un-wanted CGImageProperty, like the internal CGImageSourceRef in iOS 15+
3339
// However, CGImageCreateCopy still keep those CGImageProperty, not suit for our use case
@@ -282,6 +288,18 @@ @implementation SDImageIOAnimatedCoder {
282288
BOOL _decodeToHDR;
283289
}
284290

291+
#if SD_IMAGEIO_HDR_ENCODING
292+
+ (void)initialize {
293+
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
294+
// Use SDK instead of raw value
295+
kSDCGImageDestinationEncodeRequest = (__bridge NSString *)kCGImageDestinationEncodeRequest;
296+
kSDCGImageDestinationEncodeToSDR = (__bridge NSString *)kCGImageDestinationEncodeToSDR;
297+
kSDCGImageDestinationEncodeToISOHDR = (__bridge NSString *)kCGImageDestinationEncodeToISOHDR;
298+
kSDCGImageDestinationEncodeToISOGainmap = (__bridge NSString *)kCGImageDestinationEncodeToISOGainmap;
299+
}
300+
}
301+
#endif
302+
285303
- (void)dealloc
286304
{
287305
if (_imageSource) {
@@ -902,11 +920,11 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
902920
}
903921
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
904922
if (encodeToHDR == 1) {
905-
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToISOHDR;
923+
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToISOHDR;
906924
} else if (encodeToHDR == 2) {
907-
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToISOGainmap;
925+
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToISOGainmap;
908926
} else {
909-
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToSDR;
927+
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToSDR;
910928
}
911929
}
912930

SDWebImage/Core/SDImageIOCoder.m

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
// Specify File Size for lossy format encoding, like JPEG
2020
static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestinationRequestedFileSize";
21+
// Support Xcode 15 SDK, use raw value instead of symbol
22+
static NSString * kSDCGImageDestinationEncodeRequest = @"kCGImageDestinationEncodeRequest";
23+
static NSString * kSDCGImageDestinationEncodeToSDR = @"kCGImageDestinationEncodeToSDR";
24+
static NSString * kSDCGImageDestinationEncodeToISOHDR = @"kCGImageDestinationEncodeToISOHDR";
25+
static NSString * kSDCGImageDestinationEncodeToISOGainmap = @"kCGImageDestinationEncodeToISOGainmap";
26+
2127

2228
@implementation SDImageIOCoder {
2329
size_t _width, _height;
@@ -31,6 +37,18 @@ @implementation SDImageIOCoder {
3137
BOOL _decodeToHDR;
3238
}
3339

40+
#if SD_IMAGEIO_HDR_ENCODING
41+
+ (void)initialize {
42+
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
43+
// Use SDK instead of raw value
44+
kSDCGImageDestinationEncodeRequest = (__bridge NSString *)kCGImageDestinationEncodeRequest;
45+
kSDCGImageDestinationEncodeToSDR = (__bridge NSString *)kCGImageDestinationEncodeToSDR;
46+
kSDCGImageDestinationEncodeToISOHDR = (__bridge NSString *)kCGImageDestinationEncodeToISOHDR;
47+
kSDCGImageDestinationEncodeToISOGainmap = (__bridge NSString *)kCGImageDestinationEncodeToISOGainmap;
48+
}
49+
}
50+
#endif
51+
3452
- (void)dealloc {
3553
if (_imageSource) {
3654
CFRelease(_imageSource);
@@ -388,11 +406,11 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
388406
}
389407
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
390408
if (encodeToHDR == 1) {
391-
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToISOHDR;
409+
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToISOHDR;
392410
} else if (encodeToHDR == 2) {
393-
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToISOGainmap;
411+
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToISOGainmap;
394412
} else {
395-
properties[(__bridge NSString *)kCGImageDestinationEncodeRequest] = (__bridge NSString *)kCGImageDestinationEncodeToSDR;
413+
properties[kSDCGImageDestinationEncodeRequest] = kSDCGImageDestinationEncodeToSDR;
396414
}
397415
}
398416

SDWebImage/Private/SDImageIOAnimatedCoderInternal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#import <ImageIO/ImageIO.h>
1111
#import "SDImageIOAnimatedCoder.h"
1212

13+
// Xcode 16 SDK contains HDR encoding API, but we still support Xcode 15
14+
#define SD_IMAGEIO_HDR_ENCODING (__IPHONE_OS_VERSION_MAX_ALLOWED >= 180000)
15+
1316
// AVFileTypeHEIC/AVFileTypeHEIF is defined in AVFoundation via iOS 11, we use this without import AVFoundation
1417
#define kSDUTTypeHEIC ((__bridge CFStringRef)@"public.heic")
1518
#define kSDUTTypeHEIF ((__bridge CFStringRef)@"public.heif")

Tests/Tests/SDImageCoderTests.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,11 @@ - (NSArray *)thumbnailImagesFromImageSource:(CGImageSourceRef)source API_AVAILAB
891891

892892
- (NSDictionary *)gainMapFromImageSource:(CGImageSourceRef)source {
893893
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
894-
CFDictionaryRef HDRGainMap = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeHDRGainMap);
895894
CFDictionaryRef ISOGainMap = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeISOGainMap);
895+
CFDictionaryRef HDRGainMap = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeHDRGainMap);
896896
NSDictionary *result = ISOGainMap ? (__bridge_transfer NSDictionary *)ISOGainMap : (__bridge_transfer NSDictionary *)HDRGainMap;
897+
if (HDRGainMap) CFRelease(HDRGainMap);
898+
if (ISOGainMap) CFRelease(ISOGainMap);
897899
return result;
898900
} else {
899901
return nil;

0 commit comments

Comments
 (0)