Skip to content

Commit 4a7cd64

Browse files
authoredJul 17, 2020
feat(ios): allow for for per-image-request-headers (#691)
* Bump the dependency of SDWebImage into 5.8+ because of the SDWebImageDownloaderRequestModifier convenient API usage * Fix the implementation on iOS for per-image-request-header-setup. Should not use `SDWebImageDownloader` which manage the global shared headers. Use the context option of request modifier. * Add the support for data:image URL for WebP images * Alaways bypass invalid SSL certificate error, same behavior like React-Native itself * Fix the implementation of request modifier on SDWebImage, which have bugs on 5.8.0 (will fix in 5.8.1), revert the dependency bump * Revert the changes to bypass SSL error * Remove the unused Podfile.lock changes
1 parent 0e29d83 commit 4a7cd64

File tree

3 files changed

+19
-414
lines changed

3 files changed

+19
-414
lines changed
 

‎.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ DerivedData
2222
*.xcuserstate
2323
project.xcworkspace
2424

25+
# CocoaPods
26+
Podfile.lock
27+
Pods
28+
2529
# Android/IJ
2630
#
2731
.idea

‎ReactNativeFastImageExample/ios/Podfile.lock

-409
This file was deleted.

‎ios/FastImage/FFFastImageView.m

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "FFFastImageView.h"
2+
#import <SDWebImage/UIImage+MultiFormat.h>
23

34
@interface FFFastImageView()
45

@@ -124,7 +125,8 @@ - (void)reloadImage
124125
} {
125126
self.hasSentOnLoadStart = NO;
126127
}
127-
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:_source.url]];
128+
// Use SDWebImage API to support external format like WebP images
129+
UIImage *image = [UIImage sd_imageWithData:[NSData dataWithContentsOfURL:_source.url]];
128130
[self setImage:image];
129131
if (self.onFastImageProgress) {
130132
self.onFastImageProgress(@{
@@ -142,9 +144,16 @@ - (void)reloadImage
142144
}
143145

144146
// Set headers.
145-
[_source.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString* header, BOOL *stop) {
146-
[[SDWebImageDownloader sharedDownloader] setValue:header forHTTPHeaderField:key];
147+
NSDictionary *headers = _source.headers;
148+
SDWebImageDownloaderRequestModifier *requestModifier = [SDWebImageDownloaderRequestModifier requestModifierWithBlock:^NSURLRequest * _Nullable(NSURLRequest * _Nonnull request) {
149+
NSMutableURLRequest *mutableRequest = [request mutableCopy];
150+
for (NSString *header in headers) {
151+
NSString *value = headers[header];
152+
[mutableRequest setValue:value forHTTPHeaderField:header];
153+
}
154+
return [mutableRequest copy];
147155
}];
156+
SDWebImageContext *context = @{SDWebImageContextDownloadRequestModifier : requestModifier};
148157

149158
// Set priority.
150159
SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageHandleCookies;
@@ -180,15 +189,16 @@ - (void)reloadImage
180189
self.hasCompleted = NO;
181190
self.hasErrored = NO;
182191

183-
[self downloadImage:_source options:options];
192+
[self downloadImage:_source options:options context:context];
184193
}
185194
}
186195

187-
- (void)downloadImage:(FFFastImageSource *) source options:(SDWebImageOptions) options {
196+
- (void)downloadImage:(FFFastImageSource *) source options:(SDWebImageOptions) options context:(SDWebImageContext *)context {
188197
__weak typeof(self) weakSelf = self; // Always use a weak reference to self in blocks
189198
[self sd_setImageWithURL:_source.url
190199
placeholderImage:nil
191200
options:options
201+
context:context
192202
progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
193203
if (weakSelf.onFastImageProgress) {
194204
weakSelf.onFastImageProgress(@{

0 commit comments

Comments
 (0)
Please sign in to comment.