设置下载器的请求头:
V4设置方式:
SDWebImageDownloader *downloader = [SDWebImageManager sharedManager].imageDownloader;
[downloader setValue:@"foo=bar" forHTTPHeaderField:@"Cookie"];
V5设置方式之Objective-C
SDWebImageDownloaderRequestModifier *requestModifier = [SDWebImageDownloaderRequestModifier requestModifierWithBlock:^NSURLRequest * _Nullable(NSURLRequest * _Nonnull request) {
if ([request.URL.host isEqualToString:@"foo"]) {
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest setValue:@"foo=bar" forHTTPHeaderField:@"Cookie"];
return [mutableRequest copy];
}
return request;
}];
SDWebImageDownloader.sharedDownloader.requestModifier = requestModifier;
V5设置方式之Swift
let requestModifier = SDWebImageDownloaderRequestModifier { (request) -> URLRequest? in
if (request.url?.host == "foo") {
var mutableRequest = request
mutableRequest.setValue("foo=bar", forHTTPHeaderField: "Cookie")
return mutableRequest
}
return request
};
SDWebImageDownloader.shared.requestModifier = requestModifier
或
[[SDWebImageDownloader sharedDownloader] setValue:@"requestHeader_value" forHTTPHeaderField:@"requestHeader_key"];
图片预加载
NSArray<NSURL> *imageURLs;
[[SDWebImagePrefetcher.sharedImagePrefetcher] prefetchURLs:imageURLs progress:^(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls) {
// prefetch progress changed
} completed:^(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls) {
// all provided urls prefetched
}];
或
SDWebImagePrefetcher *prefetcher = SDWebImagePrefetcher.sharedImagePrefetcher;
[prefetcher cancelPrefetching];
[prefetcher prefetchURLs:@[url1, url2]];
指示器
imageView1.sd_imageIndicator = SDWebImageActivityIndicator.grayIndicator;
imageView2.sd_imageIndicator = SDWebImageProgressIndicator.defaultIndicator;
直接使用SDWebImageManager下载
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager loadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// do something with image
}
}];
自定义缓存过滤键
SDWebImageCacheKeyFilter *cacheKeyFilter = [SDWebImageCacheKeyFilter cacheKeyFilterWithBlock:^NSString * _Nullable(NSURL * _Nonnull url) {
NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithURL:url resolvingAgainstBaseURL:NO];
urlComponents.query = nil;
return urlComponents.URL.absoluteString;
}];
SDWebImageManager.sharedManager.cacheKeyFilter = cacheKeyFilter;
API Changes
SDImageCache
- moved
maxMemoryCostandmaxMemoryCountLimittoSDImageCacheConfig -
makeDiskCachePath:removed, useNSSearchPathForDirectoriesInDomainswith NSString's Path API instead. -
addReadOnlyCachePath:removed, useadditionalCachePathBlockinstead -
cachePathForKey:inPath:removed, usecachePathForKey:with NSString's path API instead. -
defaultCachePathForKey:removed, usecachePathForKey:instead -
SDCacheQueryCompletedBlockrenamed toSDImageCacheQueryCompletionBlock -
SDWebImageCheckCacheCompletionBlockrenamed toSDImageCacheCheckCompletionBlock -
SDWebImageCalculateSizeBlockrenamed toSDImageCacheCalculateSizeBlock -
getSizerenamed tototalDiskSize -
getDiskCountrenamed tototalDiskCount
SDImageCacheConfig
-
shouldDecompressImagesremoved. UseSDImageCacheAvoidDecodeImagein cache options instead -
maxCacheAgerenamed tomaxDiskAge -
maxCacheSizerenamed tomaxDiskSize










网友评论