SDImageLoader

@protocol SDImageLoader <NSObject>

This is the protocol to specify custom image load process. You can create your own class to conform this protocol and use as a image loader to load image from network or any available remote resources defined by yourself. If you want to implement custom loader for image download from network or local file, you just need to concentrate on image data download only. After the download finish, call SDImageLoaderDecodeImageData or SDImageLoaderDecodeProgressiveImageData to use the built-in decoding process and produce image (Remember to call in the global queue). And finally callback the completion block. If you directly get the image instance using some third-party SDKs, such as image directly from Photos framework. You can process the image data and image instance by yourself without that built-in decoding process. And finally callback the completion block.

Note

It’s your responsibility to load the image in the desired global queue(to avoid block main queue). We do not dispatch these method call in a global queue but just from the call queue (For SDWebImageManager, it typically call from the main queue).
  • Whether current image loader supports to load the provide image URL. This will be checked every time a new image request come for loader. If this return NO, we will mark this image load as failed. If return YES, we will start to call requestImageWithURL:options:context:progress:completed:.

    Declaration

    Objective-C

    - (BOOL)canRequestImageForURL:(nullable NSURL *)url;

    Swift

    func canRequestImage(for url: URL?) -> Bool

    Parameters

    url

    The image URL to be loaded.

    Return Value

    YES to continue download, NO to stop download.

  • Load the image and image data with the given URL and return the image data. You’re responsible for producing the image instance.

    • - note: the progress block is executed on a background queue

    Declaration

    Objective-C

    - (nullable id<SDWebImageOperation>)
        requestImageWithURL:(nullable NSURL *)url
                    options:(SDWebImageOptions)options
                    context:(nullable SDWebImageContext *)context
                   progress:(nullable SDImageLoaderProgressBlock)progressBlock
                  completed:(nullable SDImageLoaderCompletedBlock)completedBlock;

    Swift

    func requestImage(with url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]?, progress progressBlock: SDImageLoaderProgressBlock?, completed completedBlock: SDImageLoaderCompletedBlock? = nil) -> SDWebImageOperation?

    Parameters

    url

    The URL represent the image. Note this may not be a HTTP URL

    options

    A mask to specify options to use for this request

    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.

    progressBlock

    A block called while image is downloading

    completedBlock

    A block called when operation has been completed.

    Return Value

    An operation which allow the user to cancel the current request.

  • Whether the error from image loader should be marked indeed un-recoverable or not. If this return YES, failed URL which does not using SDWebImageRetryFailed will be blocked into black list. Else not.

    Declaration

    Objective-C

    - (BOOL)shouldBlockFailedURLWithURL:(nonnull NSURL *)url
                                  error:(nonnull NSError *)error;

    Swift

    func shouldBlockFailedURL(with url: URL, error: Error) -> Bool

    Parameters

    url

    The URL represent the image. Note this may not be a HTTP URL

    error

    The URL’s loading error, from previous requestImageWithURL:options:context:progress:completed: completedBlock’s error.

    Return Value

    Whether to block this url or not. Return YES to mark this URL as failed.