FormatIndicatedCacheSerializer

public struct FormatIndicatedCacheSerializer : CacheSerializer

FormatIndicatedCacheSerializer lets you indicate an image format for serialized caches.

It could serialize and deserialize PNG, JPEG and GIF images. For image other than these formats, a normalized pngRepresentation will be used.

Example:

let profileImageSize = CGSize(width: 44, height: 44)

// A round corner image.
let imageProcessor = RoundCornerImageProcessor(
    cornerRadius: profileImageSize.width / 2, targetSize: profileImageSize)

let optionsInfo: KingfisherOptionsInfo = [
    .cacheSerializer(FormatIndicatedCacheSerializer.png), 
    .processor(imageProcessor)]

A URL pointing to a JPEG image.
let url = URL(string: "https://example.com/image.jpg")!

// Image will be always cached as PNG format to preserve alpha channel for round rectangle.
// So when you load it from cache again later, it will be still round cornered.
// Otherwise, the corner part would be filled by white color (since JPEG does not contain an alpha channel).
imageView.kf.setImage(with: url, options: optionsInfo)
  • png

    A FormatIndicatedCacheSerializer which converts image from and to PNG format. If the image cannot be represented by PNG format, it will fallback to its real format which is determined by original data.

    Declaration

    Swift

    public static let png: FormatIndicatedCacheSerializer
  • A FormatIndicatedCacheSerializer which converts image from and to JPEG format. If the image cannot be represented by JPEG format, it will fallback to its real format which is determined by original data. The compression quality is 1.0 when using this serializer. If you need to set a customized compression quality, use jpeg(compressionQuality:).

    Declaration

    Swift

    public static let jpeg: FormatIndicatedCacheSerializer
  • A FormatIndicatedCacheSerializer which converts image from and to JPEG format with a settable compression quality. If the image cannot be represented by JPEG format, it will fallback to its real format which is determined by original data.

    Declaration

    Swift

    public static func jpeg(compressionQuality: CGFloat) -> FormatIndicatedCacheSerializer

    Parameters

    compressionQuality

    The compression quality when converting image to JPEG data.

  • gif

    A FormatIndicatedCacheSerializer which converts image from and to GIF format. If the image cannot be represented by GIF format, it will fallback to its real format which is determined by original data.

    Declaration

    Swift

    public static let gif: FormatIndicatedCacheSerializer
  • Creates data which represents the given image under a format.

    Declaration

    Swift

    public func data(with image: KFCrossPlatformImage, original: Data?) -> Data?
  • Same implementation as DefaultCacheSerializer.

    Declaration

    Swift

    public func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage?