SDWebImage 4.0 is the latest major release of SDWebImage, a top library for downloading and caching images. As a major release, following Semantic Versioning conventions, 4.0 introduces several API-breaking changes with its new architecture.
We've expanded the list of supported platforms and added to the existing iOS and tvOS, the long waited watchOS and Mac OS X.
Our support for animated images (especially GIFs) was not that great, so we decided to delegate this responsibility to FLAnimatedImage, a library created by Flipboard which has great results in working with animated images.
This guide is provided in order to ease the transition of existing applications using SDWebImage 3.X to the latest APIs, as well as explain the design and structure of new and changed functionality.
SDWebImage 4.0 officially supports iOS 7 and later, Mac OS X 10.8 and later, watchOS 2 and later and tvOS 9 and later. It needs Xcode 7.3 or later to be able to build everything properly.
For targeting previous versions of the SDKs, check README - Backwards compatibility .
Because the 4.0 version included #1581 - Lightweight Generics and Nullability, the Swift interface for all users has changed.
For 3.x versions which did not have the Nullability specifiers, all params and vars where bridged as Implicitly Unwrapped Optionals.
With 4.0, the ones marked as nullable will be regular optionals, as the nonnull ones are non-optionals.
For details, read Nullability and Objective-C.
SDWebImage 3.x
[imageView sd_setImageWithURL:url placeholderImage:placeholderImage];
SDWebImage 4.x
[imageView sd_setImageWithURL:url placeholderImage:placeholderImage];
SDWebImage 3.x
[manager downloadImageWithURL:url options:options: progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { ... } ];
SDWebImage 4.x
[manager loadImageWithURL:url options:options: progress:nil completed:^(UIImage *image, NSData *imageData, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { ... } ];
SDImageCacheConfigclass forSDImageCacheconfig (shouldDecompressImages,shouldDisableiCloud,shouldCacheImagesInMemory,maxCacheAge,maxCacheSize)SDWebImageDownloadTokenclass for the ability to cancel specific downloads (url,downloadOperationCancelToken)UIView (WebCache)category because of DRY, with methodssd_imageURLsd_internalSetImageWithURL:placeholderImage:options:operationKey:setImageBlock:progress:completed:sd_cancelCurrentImageLoadsd_showActivityIndicatorViewsd_addActivityIndicatorsd_removeActivityIndicator
SDWebImageDownloaderOperationInterfaceprotocol to describe the downloader operation behavior (in case one wants to customize)SDImageFormatenum containing the formats supported by the library (jpeg, png, gif, tiff, webp)FLAnimatedImageView (WebCache)category forFLAnimatedImageViewfrom FLAnimatedImage
- moved to
UIView (WebCache),UIImageViewobjects still respond to those methods:sd_imageURLsd_cancelCurrentImageLoadsetShowActivityIndicatorView:renamed tosd_setShowActivityIndicatorView:setIndicatorStyle:renamed tosd_setIndicatorStyle:
- removed deprecated methods:
imageURLsetImageWithURL:setImageWithURL:placeholderImage:setImageWithURL:placeholderImage:options:setImageWithURL:completed:setImageWithURL:placeholderImage:completed:setImageWithURL:placeholderImage:options:completed:setImageWithURL:placeholderImage:options:progress:completed:sd_setImageWithPreviousCachedImageWithURL:andPlaceholderImage:options:progress:completed:setAnimationImagesWithURLs:cancelCurrentArrayLoadcancelCurrentImageLoad
- removed deprecated methods:
currentImageURLimageURLForState:setImageWithURL:forState:setImageWithURL:forState:placeholderImage:setImageWithURL:forState:placeholderImage:options:setImageWithURL:forState:completed:setImageWithURL:forState:placeholderImage:completed:setImageWithURL:forState:placeholderImage:options:completed:setBackgroundImageWithURL:forState:setBackgroundImageWithURL:forState:placeholderImage:setBackgroundImageWithURL:forState:placeholderImage:options:setBackgroundImageWithURL:forState:completed:setBackgroundImageWithURL:forState:placeholderImage:completed:setBackgroundImageWithURL:forState:placeholderImage:options:completed:cancelCurrentImageLoadcancelBackgroundImageLoadForState:
- removed deprecated methods:
imageURLsetImageWithURL:setImageWithURL:placeholderImage:setImageWithURL:placeholderImage:options:setImageWithURL:completed:setImageWithURL:placeholderImage:completed:setImageWithURL:placeholderImage:options:completed:cancelCurrentImageLoad
- removed methods:
sd_cancelCurrentHighlightedImageLoad
- removed deprecated methods:
setHighlightedImageWithURL:setHighlightedImageWithURL:options:setHighlightedImageWithURL:completed:setHighlightedImageWithURL:options:completed:setHighlightedImageWithURL:options:progress:completed:cancelCurrentHighlightedImageLoad
initWithCache:downloader:is now the designated initializer- added
SDWebImageScaleDownLargeImagesoption for scaling large images - renamed
downloadImageWithURL:options:progress:completedtoloadImageWithURL:options:progress:completedjust to make it clear what the method does - renamed
SDWebImageCompletionBlocktoSDExternalCompletionBlock - renamed
SDWebImageCompletionWithFinishedBlocktoSDInternalCompletionBlockand added extraNSDataparam - removed synchronous methods:
cachedImageExistsForURL:diskImageExistsForURL:
- removed deprecated methods:
downloadWithURL:options:progress:completed:
- removed deprecated types:
SDWebImageCompletedBlockSDWebImageCompletedWithFinishedBlock
initWithImageManager:is now the designated initializer
- added
initWithSessionConfiguration:which is now the designated initializer - added
SDWebImageDownloaderScaleDownLargeImagesoption for scaling large images - added a
NSURLparam toSDWebImageDownloaderProgressBlock downloadImageWithURL:options:progress:completed:now returns aSDWebImageDownloadToken- added method
cancel:which takes aSDWebImageDownloadToken
initWithRequest:inSession:options:progress:completed:cancelled:replaced by two methods:initWithRequest:inSession:options:andaddHandlersForProgress:completed:initWithRequest:inSession:options:is now the designated initializer- added
cancel:method - removed deprecated methods:
initWithRequest:options:progress:completed:cancelled:
- moved the following properties to
SDImageCacheConfig:shouldDecompressImagesshouldDisableiCloudshouldCacheImagesInMemorymaxCacheAgemaxCacheSize
- added a
configproperty (SDImageCacheConfig) - renamed
SDWebImageQueryCompletedBlocktoSDCacheQueryCompletedBlockand addedNSDataparam initWithNamespace:diskCacheDirectory:is now the designated initializer- the
storeImage:forKey:,storeImage:forKey:toDisk:,storeImage:recalculateFromImage:imageData:forKey:toDisk:methods were async already, but declared as sync. Properly marked them as async + addedcompletionparam. Got rid of therecalculateparam. If theNSDatais provided, use it. Otherwise, recalculate it from theUIImagestoreImage:forKey:->storeImage:forKey:completion:storeImage:forKey:toDisk:->storeImage:forKey:toDisk:completion:storeImage:recalculateFromImage:imageData:forKey:toDisk:->storeImage:imageData:forKey:toDisk:completion:
- removed the synchronous method
diskImageExistsWithKey: - got rid of the confusion caused by having
cleanDiskandclearDisk. RenamedcleanDiskWithCompletion:todeleteOldFilesWithCompletion:. - removed the synchronous
clearDiskanddeleteOldFiles - renamed
queryDiskCacheForKey:done:toqueryCacheOperationForKey:done: - another clarification:
imageFromDiskCacheForKey:used to also check the memory cache which I think is misleading. NowimageFromDiskCacheForKey:only checks the disk cache and the new methodimageFromCacheForKey:checks both caches - removed
removeImageForKey:andremoveImageForKey:fromDisk:because they caused confusion (were calling the async ones withnilascompletion)
- renamed
sd_contentTypeForImageData:tosd_imageFormatForImageData:and returnsSDImageFormat - removed the deprecated method
contentTypeForImageData:
- removed
dispatch_main_sync_safeas it could be mistakenly used - updated
dispatch_main_async_safeso it checks for the main queue instead of the main thread
- added
decodedAndScaledDownImageWithImage:that decodes the image and scales it down if it's too big (over 60MB in memory)
- removed
sd_animatedGIFNamed:orsd_animatedImageByScalingAndCroppingToSize: - added
isGIF - added
sd_imageDataandsd_imageDataAsFormat:. Those methods transform aUIImageto theNSDatarepresentation