Skip to content

Commit b391207

Browse files
authored
Merge pull request #7 from SDWebImage/feature_request_image_data
Feature: request image data context option to always request image data
2 parents 82df99e + efe1960 commit b391207

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

Example/Tests/SDPhotosPluginTests.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ - (void)testUIImageViewSetImageWithGIFAsset {
9797
context:@{SDWebImageContextCustomManager : manager}
9898
progress:nil
9999
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
100+
// Strong retain imageView to avoid immediatelly dealloc
101+
expect(imageView.image).equal(image);
100102
// Expect animated image
101103
expect(image.sd_isAnimated).to.beTruthy();
102104
[expectation fulfill];
103105
}];
104106
});
105107
}];
106108

107-
[self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
109+
[self waitForExpectationsWithTimeout:kAsyncTestTimeout * 2 handler:nil];
108110
}
109111

110112
#pragma mark - Util

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ imageView.sd_setImage(with: photosURL, placeholderImage: nil, context:[.photosIm
155155

156156
1. Since Photos Library image is already stored on the device disk. And query speed is fast enough for small resolution image. You can use `SDWebImageContextStoreCacheType` with `SDImageCacheTypeNone` to disable cache storage. And use `SDWebImageFromLoaderOnly` to disable cache query.
157157
2. If you use `PHImageRequestOptionsDeliveryModeOpportunistic` (by default) to load the image, PhotosKit will return a degraded thumb image firstly and again with the full pixel image. When the image is degraded, the loader completion block will set `finished = NO`. But this will not trigger the View Category completion block, only trigger a image refresh (like progressive loading behavior for network image using `SDWebImageProgressiveLoad`)
158+
3. By default, we will prefer using Photos [requestImageForAsset:targetSize:contentMode:options:resultHandler:](https://developer.apple.com/documentation/photokit/phimagemanager/1616964-requestimageforasset?language=objc) API for normal images, using [requestImageDataForAsset:options:resultHandler:](https://developer.apple.com/documentation/photokit/phimagemanager/1616957-requestimagedataforasset?language=objc) for animated images like GIF asset. If you need the raw image data for further image processing, you can always pass `SDWebImageContextPhotosRequestImageData` context option to force using the request data API instead. Note when request data, the `targetSize` and `contentMode` options are ignored. If you need smaller image size, consider using [Image Transformer](https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage#image-transformer-50) feature from SDWebImage 5.0
158159

159160
## Warning
160161

SDWebImagePhotosPlugin/Classes/SDWebImagePhotosDefine.h

+8
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPhotos
4040
A PHImageRequestOptions instance used in the Photos Library image request process. If you do not provide, we will use the default options to fetch the image (PHImageRequestOptions)
4141
*/
4242
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPhotosImageRequestOptions;
43+
44+
/**
45+
A Bool value specify whether or not, to use `requestImageDataForAsset:options:resultHandler:` instead of `requestImageForAsset:targetSize:contentMode:options:resultHandler:` API to request Asset's Image.
46+
By default, we automatically use `requestImageDataForAsset` for Animated Asset (GIF images), use `requestImageForAsset` for other Asset. If you provide a custom value, always using that value instead.
47+
If you care about the raw image data, you can enable this context option to get the raw image data in completion block.
48+
@note When query the raw image data, the `targetSize` and `contentMode` are ignored. You can use image transformer or other ways from your propose. See Apple's documentation for more details information. (NSNumber *)
49+
*/
50+
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPhotosRequestImageData;

SDWebImagePhotosPlugin/Classes/SDWebImagePhotosDefine.m

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515

1616
SDWebImageContextOption _Nonnull const SDWebImageContextPhotosFetchOptions = @"photosFetchOptions";
1717
SDWebImageContextOption _Nonnull const SDWebImageContextPhotosImageRequestOptions = @"photosImageRequestOptions";
18+
SDWebImageContextOption _Nonnull const SDWebImageContextPhotosRequestImageData = @"photosRequestImageData";

SDWebImagePhotosPlugin/Classes/SDWebImagePhotosLoader.m

+13-7
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,21 @@ - (BOOL)canRequestImageForURL:(NSURL *)url {
156156
return;
157157
}
158158

159-
// Check UTType
160-
NSString *uniformTypeIdentifier;
161-
if ([asset respondsToSelector:@selector(uniformTypeIdentifier)]) {
162-
uniformTypeIdentifier = [asset valueForKey:NSStringFromSelector(@selector(uniformTypeIdentifier))];
159+
// Request image data instead of image
160+
BOOL requestImageData;
161+
if (context[SDWebImageContextPhotosRequestImageData]) {
162+
requestImageData = [context[SDWebImageContextPhotosRequestImageData] boolValue];
163+
} else {
164+
// Check UTType
165+
NSString *uniformTypeIdentifier;
166+
if ([asset respondsToSelector:@selector(uniformTypeIdentifier)]) {
167+
uniformTypeIdentifier = [asset valueForKey:NSStringFromSelector(@selector(uniformTypeIdentifier))];
168+
}
169+
// Check Animated Image, which need the original image data
170+
requestImageData = [[self class] isAnimatedImageWithUTType:uniformTypeIdentifier];
163171
}
164172

165-
// Check Animated Image, which need the original image data
166-
if ([[self class] isAnimatedImageWithUTType:uniformTypeIdentifier]) {
167-
// Animated image need load raw image data
173+
if (requestImageData) {
168174
[self fetchImageDataWithAsset:asset operation:operation url:url options:options context:context progress:progressBlock completed:completedBlock];
169175
} else {
170176
[self fetchImageWithAsset:asset operation:operation url:url options:options context:context progress:progressBlock completed:completedBlock];

0 commit comments

Comments
 (0)