Skip to content

Commit 31e3741

Browse files
committed
Update README.md
Added HDR Photos code snippet
1 parent db865b0 commit 31e3741

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,52 @@ SDImagePhotosLoader.shared.requestImageAssetOnly = false
149149

150150
Then just request the PHAssets or using the fetch options, which the media type is `.video`.
151151

152+
#### HDR Photo Rendering
153+
To enable HDR decoding and rendering, upgrade SDWebImage to 5.21.0+, then, request the image data from Photos Library and turn on HDR decoding.
154+
155+
+ Objective-C
156+
157+
```objective-c
158+
#if TARGET_OS_OSX
159+
BOOL supportsHDR = NSScreen.mainScreen.maximumPotentialExtendedDynamicRangeColorComponentValue > 1.0;
160+
#else
161+
#define NSImageDynamicRangeHigh UIImageDynamicRangeHigh
162+
BOOL supportsHDR = UIScreen.mainScreen.potentialEDRHeadroom > 1.0;
163+
#endif
164+
SDWebImageMutableContext *context = [@{SDWebImageContextStoreCacheType: @(SDImageCacheTypeNone)} mutableCopy];
165+
if (supportsHDR) {
166+
if (@available (macOS 14.0, iOS 17, *)) {
167+
cell.imageViewDisplay.preferredImageDynamicRange = NSImageDynamicRangeHigh; // Enable Image View Level control for HDR
168+
}
169+
context[SDWebImageContextPhotosRequestImageData] = @(YES); // Photos Library only load HDR info when requestImageData
170+
context[SDWebImageContextImageDecodeToHDR] = @(YES); // When decoding HDR data, we need explicit enable HDR decoding
171+
}
172+
// Then loading HDR assets with context option
173+
[imageView sd_setImageWithURL:photosURL placeholderImage:nil context:context];
174+
```
175+
176+
+ Swift
177+
178+
```swift
179+
#if os(macOS)
180+
let supportsHDR = NSScreen.main.maximumPotentialExtendedDynamicRangeColorComponentValue > 1.0
181+
#else
182+
let supportsHDR = UIScreen.main.potentialEDRHeadroom > 1.0
183+
#endif
184+
var context = [
185+
SDWebImageContextStoreCacheType: SDImageCacheType.none.rawValue
186+
]
187+
if supportsHDR {
188+
if #available(macOS 14.0, iOS 17, *) {
189+
cell.imageViewDisplay.preferredImageDynamicRange = .high // Enable Image View Level control for HDR
190+
}
191+
context[.photosRequestImageData] = true // Photos Library only load HDR info when requestImageData
192+
context[.imageDecodeToHDR] = true // When decoding HDR data, we need explicit enable HDR decoding
193+
}
194+
// Then loading HDR assets with context option
195+
imageView.sd_setImage(with: photosURL, placeholderImage: nil, context:context)
196+
```
197+
152198
#### Fetch/Request Options
153199
To specify options like `PHFetchOptions` or `PHImageRequestOptions` for Photos Library. Either to change the correspond properties in loader, or provide a context options for each image request.
154200

0 commit comments

Comments
 (0)