Skip to content

Commit

Permalink
Added FormatIndicatedCacheSerializer
Browse files Browse the repository at this point in the history
FormatIndicatedCacheSerializer let you indicate an image format for
serialized caches.
  • Loading branch information
Jonny authored and Jonny committed May 31, 2017
1 parent 6368877 commit f1e50bf
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
96 changes: 96 additions & 0 deletions FormatIndicatedCacheSerializer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// RequestModifier.swift
// Kingfisher
//
// Created by Junyu Kuang on 5/28/17.
//
// Copyright (c) 2017 Wei Wang <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

/// `FormatIndicatedCacheSerializer` let you indicate an image format for serialized caches.
///
/// It could serialize and deserialize PNG, JEPG and GIF images. For
/// image other than these formats, a normalized `pngRepresentation` will be used.
///
/// Example:
/// ````
/// private let profileImageSize = CGSize(width: 44, height: 44)
///
/// private let imageProcessor = RoundCornerImageProcessor(
/// cornerRadius: profileImageSize.width / 2, targetSize: profileImageSize)
///
/// private let optionsInfo: KingfisherOptionsInfo = [
/// .cacheSerializer(FormatIndicatedCacheSerializer.png),
/// .backgroundDecode, .processor(imageProcessor), .scaleFactor(UIScreen.main.scale)]
///
/// extension UIImageView {
/// func setProfileImage(with url: URL) {
/// // Image will always cached as PNG format to preserve alpha channel for round rect.
/// _ = kf.setImage(with: url, options: optionsInfo)
/// }
///}
/// ````
public struct FormatIndicatedCacheSerializer: CacheSerializer {

public static let png = FormatIndicatedCacheSerializer(imageFormat: .PNG)
public static let jpeg = FormatIndicatedCacheSerializer(imageFormat: .JPEG)
public static let gif = FormatIndicatedCacheSerializer(imageFormat: .GIF)

/// The indicated image format.
private let imageFormat: ImageFormat

public func data(with image: Image, original: Data?) -> Data? {

func imageData(withFormat imageFormat: ImageFormat) -> Data? {
switch imageFormat {
case .PNG: return image.kf.pngRepresentation()
case .JPEG: return image.kf.jpegRepresentation(compressionQuality: 1.0)
case .GIF: return image.kf.gifRepresentation()
case .unknown: return nil
}
}

// generate data with indicated image format
if let data = imageData(withFormat: imageFormat) {
return data
}

let originalFormat = original?.kf.imageFormat ?? .unknown

// generate data with original image's format
if originalFormat != imageFormat, let data = imageData(withFormat: originalFormat) {
return data
}

return original ?? image.kf.normalized.kf.pngRepresentation()
}

/// Same implementation as `DefaultCacheSerializer`.
public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
let options = options ?? KingfisherEmptyOptionsInfo
return Kingfisher<Image>.image(
data: data,
scale: options.scaleFactor,
preloadAllAnimationData: options.preloadAllAnimationData,
onlyFirstFrame: options.onlyLoadFirstFrame)
}
}
10 changes: 10 additions & 0 deletions Kingfisher.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@
D9638BA61C7DC71F0046523D /* ImagePrefetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9638BA41C7DC71F0046523D /* ImagePrefetcherTests.swift */; };
D9638BA71C7DCF560046523D /* ImagePrefetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9638BA41C7DC71F0046523D /* ImagePrefetcherTests.swift */; };
D9638BA81C7DCF570046523D /* ImagePrefetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9638BA41C7DC71F0046523D /* ImagePrefetcherTests.swift */; };
FB402D0E1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB402D0D1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift */; };
FB402D0F1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB402D0D1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift */; };
FB402D101EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB402D0D1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift */; };
FB402D111EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB402D0D1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -747,6 +751,7 @@
D9638B9F1C7DBA660046523D /* ImagePrefetcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ImagePrefetcher.swift; path = Sources/ImagePrefetcher.swift; sourceTree = "<group>"; };
D9638BA41C7DC71F0046523D /* ImagePrefetcherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePrefetcherTests.swift; sourceTree = "<group>"; };
DE80CB18FBC9F9F23DC1FDCF /* Pods-KingfisherTests-macOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KingfisherTests-macOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KingfisherTests-macOS/Pods-KingfisherTests-macOS.release.xcconfig"; sourceTree = "<group>"; };
FB402D0D1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormatIndicatedCacheSerializer.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -1058,6 +1063,7 @@
D9638B9F1C7DBA660046523D /* ImagePrefetcher.swift */,
4B2B8E491D70128200FC4749 /* ImageProcessor.swift */,
4BB24C3C1D79215A00CD5F9C /* CacheSerializer.swift */,
FB402D0D1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift */,
4BFBEE7C1D7D0C3600699FD3 /* RequestModifier.swift */,
D10945ED1C526B6C001408EB /* ImageTransition.swift */,
D10945F11C526B6C001408EB /* KingfisherManager.swift */,
Expand Down Expand Up @@ -2319,6 +2325,7 @@
D109461F1C526C61001408EB /* KingfisherManager.swift in Sources */,
4B7742451D87E2AA0077024E /* Box.swift in Sources */,
4B6313F61D766BEF0078E017 /* Filter.swift in Sources */,
FB402D101EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift in Sources */,
182FFF781CC9ACBA004B728D /* NSButton+Kingfisher.swift in Sources */,
D10946201C526C61001408EB /* KingfisherOptionsInfo.swift in Sources */,
D10946211C526C61001408EB /* Resource.swift in Sources */,
Expand Down Expand Up @@ -2408,6 +2415,7 @@
D10946161C526C0D001408EB /* String+MD5.swift in Sources */,
4BB24C3E1D79215A00CD5F9C /* CacheSerializer.swift in Sources */,
D10946171C526C0D001408EB /* ThreadHelper.swift in Sources */,
FB402D0F1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift in Sources */,
D10946181C526C0D001408EB /* UIButton+Kingfisher.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -2417,6 +2425,7 @@
buildActionMask = 2147483647;
files = (
D109462D1C526CF5001408EB /* ImageTransition.swift in Sources */,
FB402D111EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift in Sources */,
4B2B8E4D1D70141100FC4749 /* ImageProcessor.swift in Sources */,
D10946251C526CE8001408EB /* Image.swift in Sources */,
D10946261C526CE8001408EB /* ImageCache.swift in Sources */,
Expand Down Expand Up @@ -2475,6 +2484,7 @@
D10945FF1C526B86001408EB /* String+MD5.swift in Sources */,
4BB24C3D1D79215A00CD5F9C /* CacheSerializer.swift in Sources */,
D10946001C526B86001408EB /* ThreadHelper.swift in Sources */,
FB402D0E1EDEAB7E002B62A1 /* FormatIndicatedCacheSerializer.swift in Sources */,
D10946011C526B86001408EB /* UIButton+Kingfisher.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

0 comments on commit f1e50bf

Please sign in to comment.