Functions

The following functions are available globally.

  • This is the built-in decoding process for image query from cache.

    Note

    If you want to implement your custom loader with queryImageForKey:options:context:completion: API, but also want to keep compatible with SDWebImage’s behavior, you’d better use this to produce image.

    Declaration

    Objective-C

    extern UIImage *_Nullable SDImageCacheDecodeImageData(
        NSData *_Nonnull imageData, NSString *_Nonnull cacheKey,
        SDWebImageOptions options, SDWebImageContext *_Nullable context)

    Swift

    func SDImageCacheDecodeImageData(_ imageData: Data, _ cacheKey: String, _ options: SDWebImageOptions, _ context: [SDWebImageContextOption : Any]?) -> UIImage?

    Parameters

    imageData

    The image data from the cache. Should not be nil

    cacheKey

    The image cache key from the input. Should not be nil

    options

    The options arg from the input

    context

    The context arg from the input

    Return Value

    The decoded image for current image data query from cache

  • Returns the current graphics context.

    Declaration

    Objective-C

    extern CGContextRef _Nullable SDGraphicsGetCurrentContext()

    Swift

    func SDGraphicsGetCurrentContext() -> CGContext?
  • Creates a bitmap-based graphics context and makes it the current context.

    Declaration

    Objective-C

    extern void SDGraphicsBeginImageContext(CGSize size)

    Swift

    func SDGraphicsBeginImageContext(_ size: CGSize)
  • Creates a bitmap-based graphics context with the specified options.

    Declaration

    Objective-C

    extern void SDGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque,
                                                       CGFloat scale)

    Swift

    func SDGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat)
  • Removes the current bitmap-based graphics context from the top of the stack.

    Declaration

    Objective-C

    extern void SDGraphicsEndImageContext()

    Swift

    func SDGraphicsEndImageContext()
  • Returns an image based on the contents of the current bitmap-based graphics context.

    Declaration

    Objective-C

    extern UIImage *_Nullable SDGraphicsGetImageFromCurrentImageContext()

    Swift

    func SDGraphicsGetImageFromCurrentImageContext() -> UIImage?

Helper method

  • This is the built-in decoding process for image download from network or local file.

    Note

    If you want to implement your custom loader with requestImageWithURL:options:context:progress:completed: API, but also want to keep compatible with SDWebImage’s behavior, you’d better use this to produce image.

    Declaration

    Objective-C

    extern UIImage *_Nullable SDImageLoaderDecodeImageData(
        NSData *_Nonnull imageData, NSURL *_Nonnull imageURL,
        SDWebImageOptions options, SDWebImageContext *_Nullable context)

    Swift

    func SDImageLoaderDecodeImageData(_ imageData: Data, _ imageURL: URL, _ options: SDWebImageOptions, _ context: [SDWebImageContextOption : Any]?) -> UIImage?

    Parameters

    imageData

    The image data from the network. Should not be nil

    imageURL

    The image URL from the input. Should not be nil

    options

    The options arg from the input

    context

    The context arg from the input

    Return Value

    The decoded image for current image data load from the network

  • This is the built-in decoding process for image progressive download from network. It’s used when SDWebImageProgressiveLoad option is set. (It’s not required when your loader does not support progressive image loading)

    Note

    If you want to implement your custom loader with requestImageWithURL:options:context:progress:completed: API, but also want to keep compatible with SDWebImage’s behavior, you’d better use this to produce image.

    Declaration

    Objective-C

    extern UIImage *_Nullable SDImageLoaderDecodeProgressiveImageData(
        NSData *_Nonnull imageData, NSURL *_Nonnull imageURL, BOOL finished,
        id<SDWebImageOperation> _Nonnull operation, SDWebImageOptions options,
        SDWebImageContext *_Nullable context)

    Swift

    func SDImageLoaderDecodeProgressiveImageData(_ imageData: Data, _ imageURL: URL, _ finished: Bool, _ operation: SDWebImageOperation, _ options: SDWebImageOptions, _ context: [SDWebImageContextOption : Any]?) -> UIImage?

    Parameters

    imageData

    The image data from the network so far. Should not be nil

    imageURL

    The image URL from the input. Should not be nil

    finished

    Pass NO to specify the download process has not finished. Pass YES when all image data has finished.

    operation

    The loader operation associated with current progressive download. Why to provide this is because progressive decoding need to store the partial decoded context for each operation to avoid conflict. You should provide the operation from loadImageWithURL: method return value.

    options

    The options arg from the input

    context

    The context arg from the input

    Return Value

    The decoded progressive image for current image data load from the network

  • Return the transformed cache key which applied with specify transformerKey.

    Declaration

    Objective-C

    extern NSString *_Nullable SDTransformedKeyForKey(
        NSString *_Nullable key, NSString *_Nonnull transformerKey)

    Swift

    func SDTransformedKeyForKey(_ key: String?, _ transformerKey: String) -> String?

    Parameters

    key

    The original cache key

    transformerKey

    The transformer key from the transformer

    Return Value

    The transformed cache key

  • Return the thumbnailed cache key which applied with specify thumbnailSize and preserveAspectRatio control.

    Note

    If you have both transformer and thumbnail applied for image, call SDThumbnailedKeyForKey firstly and then with SDTransformedKeyForKey.`

    Declaration

    Objective-C

    extern NSString *_Nullable SDThumbnailedKeyForKey(NSString *_Nullable key,
                                                      CGSize thumbnailPixelSize,
                                                      BOOL preserveAspectRatio)

    Swift

    func SDThumbnailedKeyForKey(_ key: String?, _ thumbnailPixelSize: CGSize, _ preserveAspectRatio: Bool) -> String?

    Parameters

    key

    The original cache key

    thumbnailPixelSize

    The thumbnail pixel size

    preserveAspectRatio

    The preserve aspect ratio option

    Return Value

    The thumbnailed cache key

Image scale

  • Return the image scale factor for the specify key, supports file name and url key. This is the built-in way to check the scale factor when we have no context about it. Because scale factor is not stored in image data (It’s typically from filename). However, you can also provide custom scale factor as well, see SDWebImageContextImageScaleFactor.

    Declaration

    Objective-C

    extern CGFloat SDImageScaleFactorForKey(NSString *_Nullable key)

    Swift

    func SDImageScaleFactorForKey(_ key: String?) -> CGFloat

    Parameters

    key

    The image cache key

    Return Value

    The scale factor for image

  • Scale the image with the scale factor for the specify key. If no need to scale, return the original image. This works for UIImage(UIKit) or NSImage(AppKit). And this function also preserve the associated value in UIImage+Metadata.h.

    Note

    This is actually a convenience function, which firstly call SDImageScaleFactorForKey and then call SDScaledImageForScaleFactor, kept for backward compatibility.

    Declaration

    Objective-C

    extern UIImage *_Nullable SDScaledImageForKey(NSString *_Nullable key,
                                                  UIImage *_Nullable image)

    Swift

    func SDScaledImageForKey(_ key: String?, _ image: UIImage?) -> UIImage?

    Parameters

    key

    The image cache key

    image

    The image

    Return Value

    The scaled image

  • Scale the image with the scale factor. If no need to scale, return the original image. This works for UIImage(UIKit) or NSImage(AppKit). And this function also preserve the associated value in UIImage+Metadata.h.

    Declaration

    Objective-C

    extern UIImage *_Nullable SDScaledImageForScaleFactor(CGFloat scale,
                                                          UIImage *_Nullable image)

    Swift

    func SDScaledImageForScaleFactor(_ scale: CGFloat, _ image: UIImage?) -> UIImage?

    Parameters

    scale

    The image scale factor

    image

    The image

    Return Value

    The scaled image