UIView(WebCache)

@interface UIView (WebCache)

Integrates SDWebImage async downloading and caching of remote images with UIView subclass.

  • Get the current image URL.

    Note

    Note that because of the limitations of categories this property can get out of sync if you use setImage: directly.

    Declaration

    Objective-C

    @property (readonly, strong, nonatomic, nullable) NSURL *sd_imageURL;

    Swift

    var sd_imageURL: URL? { get }
  • Get the current image operation key. Operation key is used to identify the different queries for one view instance (like UIButton). See more about this in SDWebImageContextSetImageOperationKey.

    Note

    You can use method UIView+WebCacheOperation to investigate different queries’ operation.

    Declaration

    Objective-C

    @property (readonly, strong, nonatomic, nullable) NSString *sd_latestOperationKey;

    Swift

    var sd_latestOperationKey: String? { get }
  • The current image loading progress associated to the view. The unit count is the received size and excepted size of download. The totalUnitCount and completedUnitCount will be reset to 0 after a new image loading start (change from current queue). And they will be set to SDWebImageProgressUnitCountUnknown if the progressBlock not been called but the image loading success to mark the progress finished (change from main queue).

    Note

    You can use Key-Value Observing on the progress, but you should take care that the change to progress is from a background queue during download(the same as progressBlock). If you want to using KVO and update the UI, make sure to dispatch on the main queue. And it’s recommend to use some KVO libs like KVOController because it’s more safe and easy to use.

    Note

    The getter will create a progress instance if the value is nil. But by default, we don’t create one. If you need to use Key-Value Observing, you must trigger the getter or set a custom progress instance before the loading start. The default value is nil.

    Note

    Note that because of the limitations of categories this property can get out of sync if you update the progress directly.

    Declaration

    Objective-C

    @property (strong, nonatomic, null_resettable) NSProgress *sd_imageProgress;

    Swift

    var sd_imageProgress: Progress! { get set }
  • Set the imageView image with an url and optionally a placeholder image.

    The download is asynchronous and cached.

    The forth parameter is an SDImageCacheType enum indicating if the image was retrieved from the local cache or from the memory cache or from the network.

    The fifth parameter normally is always YES. However, if you provide SDWebImageAvoidAutoSetImage with SDWebImageProgressiveLoad options to enable progressive downloading and set the image yourself. This block is thus called repeatedly with a partial image. When image is fully downloaded, the block is called a last time with the full image and the last parameter set to YES.

    The last parameter is the original image URL

    Declaration

    Objective-C

    - (void)sd_internalSetImageWithURL:(nullable NSURL *)url
                      placeholderImage:(nullable UIImage *)placeholder
                               options:(SDWebImageOptions)options
                               context:(nullable SDWebImageContext *)context
                         setImageBlock:(nullable SDSetImageBlock)setImageBlock
                              progress:
                                  (nullable SDImageLoaderProgressBlock)progressBlock
                             completed:
                                 (nullable SDInternalCompletionBlock)completedBlock;

    Swift

    func sd_internalSetImage(with url: URL?, placeholderImage placeholder: UIImage?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]?, setImageBlock: SDSetImageBlock?, progress progressBlock: SDImageLoaderProgressBlock?, completed completedBlock: SDInternalCompletionBlock? = nil)

    Parameters

    url

    The url for the image.

    placeholder

    The image to be set initially, until the image request finishes.

    options

    The options to use when downloading the image. - see: SDWebImageOptions for the possible values.

    context

    A context contains different options to perform specify changes or processes, see SDWebImageContextOption. This hold the extra objects which options enum can not hold.

    setImageBlock

    Block used for custom set image code. If not provide, use the built-in set image code (supports UIImageView/NSImageView and UIButton/NSButton currently)

    progressBlock

    A block called while image is downloading - note: the progress block is executed on a background queue

    completedBlock

    A block called when operation has been completed. This block has no return value and takes the requested UIImage as first parameter and the NSData representation as second parameter. In case of error the image parameter is nil and the third parameter may contain an NSError.

  • Cancel the current image load

    Declaration

    Objective-C

    - (void)sd_cancelCurrentImageLoad;

    Swift

    func sd_cancelCurrentImageLoad()

Image Transition

Image Indicator

  • The image indicator during the image loading. If you do not need indicator, specify nil. Defaults to nil The setter will remove the old indicator view and add new indicator view to current view’s subview.

    Note

    Because this is UI related, you should access only from the main queue.

    Declaration

    Objective-C

    @property (strong, nonatomic, nullable) id<SDWebImageIndicator> sd_imageIndicator;

    Swift

    var sd_imageIndicator: SDWebImageIndicator? { get set }