Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kf namespacing #435

Merged
merged 5 commits into from
Sep 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Demo/Kingfisher-Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ extension ViewController {

override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
// This will cancel all unfinished downloading task when the cell disappearing.
(cell as! CollectionViewCell).cellImageView.kf_cancelDownloadTask()
(cell as! CollectionViewCell).cellImageView.kf.cancelDownloadTask()
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell

cell.cellImageView.kf_indicatorType = .activity
cell.cellImageView.kf.indicatorType = .activity
let url = URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.row + 1).jpg")!

_ = cell.cellImageView.kf_setImage(with: url,
_ = cell.cellImageView.kf.setImage(with: url,
placeholder: nil,
options: [.transition(ImageTransition.fade(1))],
progressBlock: { receivedSize, totalSize in
Expand Down
4 changes: 2 additions & 2 deletions Demo/Kingfisher-macOS-Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ extension ViewController: NSCollectionViewDataSource {

let url = URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.item + 1).jpg")!

item.imageView?.kf_indicatorType = .activity
item.imageView?.kf_setImage(with: url, placeholder: nil, options: nil,
item.imageView?.kf.indicatorType = .activity
item.imageView?.kf.setImage(with: url, placeholder: nil, options: nil,
progressBlock: { receivedSize, totalSize in
print("\(indexPath.item + 1): \(receivedSize)/\(totalSize)")
},
Expand Down
54 changes: 54 additions & 0 deletions Kingfisher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Kingfisher.swift
// Kingfisher
//
// Created by WANG WEI on 2016/09/14.
// Copyright © 2016年 Wei Wang. All rights reserved.
//

import Foundation
import ImageIO

#if os(macOS)
import AppKit
public typealias Image = NSImage
public typealias Color = NSColor
public typealias ImageView = NSImageView
typealias Button = NSButton
#else
import UIKit
public typealias Image = UIImage
public typealias Color = UIColor
#if !os(watchOS)
public typealias ImageView = UIImageView
typealias Button = UIButton
#endif
#endif

public struct Kingfisher<Base> {
public let base: Base
public init(_ base: Base) {
self.base = base
}
}

/**
A type that has Kingfisher extensions.
*/
public protocol KingfisherCompatible {
associatedtype CompatibleType
var kf: Kingfisher<CompatibleType> { get set }
}

public extension KingfisherCompatible {
public var kf: Kingfisher<Self> {
get { return Kingfisher(self) }
set { }
}
}

extension Image: KingfisherCompatible { }
#if !os(watchOS)
extension ImageView: KingfisherCompatible { }
extension Button: KingfisherCompatible { }
#endif
10 changes: 10 additions & 0 deletions Kingfisher.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
4B7742481D87E42E0077024E /* loader.gif in Resources */ = {isa = PBXBuildFile; fileRef = 4B7742461D87E42E0077024E /* loader.gif */; };
4B98674F1CD1CF42003ADAC7 /* AnimatedImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B98674E1CD1CF42003ADAC7 /* AnimatedImageView.swift */; };
4B9867501CD1CF42003ADAC7 /* AnimatedImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B98674E1CD1CF42003ADAC7 /* AnimatedImageView.swift */; };
4BA6E2101D891C1D000ED91A /* Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BA6E20F1D891C1D000ED91A /* Kingfisher.swift */; };
4BA6E2111D892AB0000ED91A /* Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BA6E20F1D891C1D000ED91A /* Kingfisher.swift */; };
4BA6E2121D892AB1000ED91A /* Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BA6E20F1D891C1D000ED91A /* Kingfisher.swift */; };
4BA6E2131D892AB2000ED91A /* Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BA6E20F1D891C1D000ED91A /* Kingfisher.swift */; };
4BB24C3D1D79215A00CD5F9C /* CacheSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB24C3C1D79215A00CD5F9C /* CacheSerializer.swift */; };
4BB24C3E1D79215A00CD5F9C /* CacheSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB24C3C1D79215A00CD5F9C /* CacheSerializer.swift */; };
4BB24C3F1D79215A00CD5F9C /* CacheSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB24C3C1D79215A00CD5F9C /* CacheSerializer.swift */; };
Expand Down Expand Up @@ -488,6 +492,7 @@
4B7742421D87E2AA0077024E /* Box.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Box.swift; path = Sources/Box.swift; sourceTree = "<group>"; };
4B7742461D87E42E0077024E /* loader.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = loader.gif; sourceTree = "<group>"; };
4B98674E1CD1CF42003ADAC7 /* AnimatedImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AnimatedImageView.swift; path = Sources/AnimatedImageView.swift; sourceTree = "<group>"; };
4BA6E20F1D891C1D000ED91A /* Kingfisher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Kingfisher.swift; sourceTree = "<group>"; };
4BB24C3C1D79215A00CD5F9C /* CacheSerializer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CacheSerializer.swift; path = Sources/CacheSerializer.swift; sourceTree = "<group>"; };
4BCCF3361D5B02F8003387C2 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4BCCF3371D5B02F8003387C2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -895,6 +900,7 @@
D10945F21C526B6C001408EB /* KingfisherOptionsInfo.swift */,
4B6313F31D766BEF0078E017 /* Filter.swift */,
D10945F31C526B6C001408EB /* Resource.swift */,
4BA6E20F1D891C1D000ED91A /* Kingfisher.swift */,
);
name = Core;
sourceTree = "<group>";
Expand Down Expand Up @@ -2070,6 +2076,7 @@
D10946211C526C61001408EB /* Resource.swift in Sources */,
4BB24C3F1D79215A00CD5F9C /* CacheSerializer.swift in Sources */,
D9638BA21C7DBA660046523D /* ImagePrefetcher.swift in Sources */,
4BA6E2121D892AB1000ED91A /* Kingfisher.swift in Sources */,
D10946221C526C61001408EB /* String+MD5.swift in Sources */,
D10946231C526C61001408EB /* ThreadHelper.swift in Sources */,
);
Expand Down Expand Up @@ -2139,6 +2146,7 @@
4B7742441D87E2AA0077024E /* Box.swift in Sources */,
4B6313F51D766BEF0078E017 /* Filter.swift in Sources */,
4B9867501CD1CF42003ADAC7 /* AnimatedImageView.swift in Sources */,
4BA6E2111D892AB0000ED91A /* Kingfisher.swift in Sources */,
D109460F1C526C0D001408EB /* ImageCache.swift in Sources */,
D10946101C526C0D001408EB /* ImageDownloader.swift in Sources */,
4B2B8E4B1D70140F00FC4749 /* ImageProcessor.swift in Sources */,
Expand Down Expand Up @@ -2169,6 +2177,7 @@
4BFBEE801D7D0C3600699FD3 /* RequrstModifier.swift in Sources */,
D10946271C526CE8001408EB /* ImageDownloader.swift in Sources */,
D10946281C526CE8001408EB /* KingfisherManager.swift in Sources */,
4BA6E2131D892AB2000ED91A /* Kingfisher.swift in Sources */,
D10946291C526CE8001408EB /* KingfisherOptionsInfo.swift in Sources */,
D109462A1C526CE8001408EB /* Resource.swift in Sources */,
D109462B1C526CE8001408EB /* String+MD5.swift in Sources */,
Expand Down Expand Up @@ -2204,6 +2213,7 @@
4B7742431D87E2AA0077024E /* Box.swift in Sources */,
4B6313F41D766BEF0078E017 /* Filter.swift in Sources */,
4B98674F1CD1CF42003ADAC7 /* AnimatedImageView.swift in Sources */,
4BA6E2101D891C1D000ED91A /* Kingfisher.swift in Sources */,
D10945F81C526B86001408EB /* ImageCache.swift in Sources */,
D10945F91C526B86001408EB /* ImageDownloader.swift in Sources */,
4B2B8E4A1D70128200FC4749 /* ImageProcessor.swift in Sources */,
Expand Down
13 changes: 7 additions & 6 deletions Sources/AnimatedImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public class AnimatedImageView: UIImageView {
/// Reset the animator.
private func reset() {
animator = nil
if let imageSource = image?.kf_imageSource?.imageRef {
if let imageSource = image?.kf.imageSource?.imageRef {
animator = Animator(imageSource: imageSource, contentMode: contentMode, size: bounds.size, framePreloadCount: framePreloadCount)
animator?.needsPrescaling = needsPrescaling
animator?.prepareFrames()
Expand Down Expand Up @@ -258,7 +258,7 @@ class Animator {
return AnimatedFrame.null
}

let frameDuration = imageSource.kf_GIFProperties(at: index).flatMap {
let frameDuration = imageSource.kf.GIFProperties(at: index).flatMap {
gifInfo -> Double? in

let unclampedDelayTime = gifInfo[kCGImagePropertyGIFUnclampedDelayTime as String] as Double?
Expand All @@ -281,7 +281,7 @@ class Animator {
let scaledImage: Image?

if needsPrescaling {
scaledImage = image.kf_resize(to: size, for: contentMode)
scaledImage = image.kf.resize(to: size, for: contentMode)
} else {
scaledImage = image
}
Expand Down Expand Up @@ -312,9 +312,10 @@ class Animator {
}
}

extension CGImageSource {
func kf_GIFProperties(at index: Int) -> [String: Double]? {
let properties = CGImageSourceCopyPropertiesAtIndex(self, index, nil) as Dictionary?
extension CGImageSource: KingfisherCompatible { }
extension Kingfisher where Base: CGImageSource {
func GIFProperties(at index: Int) -> [String: Double]? {
let properties = CGImageSourceCopyPropertiesAtIndex(base, index, nil) as Dictionary?
return properties?[kCGImagePropertyGIFDictionary] as? [String: Double]
}
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/CacheSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public struct DefaultCacheSerializer: CacheSerializer {
private init() {}

public func data(with image: Image, original: Data?) -> Data? {
let imageFormat = original?.kf_imageFormat ?? .unknown
let imageFormat = original?.kf.imageFormat ?? .unknown

let data: Data?
switch imageFormat {
case .PNG: data = image.pngRepresentation()
case .JPEG: data = image.jpegRepresentation(compressionQuality: 1.0)
case .GIF: data = image.gifRepresentation()
case .unknown: data = original ?? image.kf_normalized().pngRepresentation()
case .PNG: data = image.kf.pngRepresentation()
case .JPEG: data = image.kf.jpegRepresentation(compressionQuality: 1.0)
case .GIF: data = image.kf.gifRepresentation()
case .unknown: data = original ?? image.kf.normalized.kf.pngRepresentation()
}

return data
Expand All @@ -80,6 +80,6 @@ public struct DefaultCacheSerializer: CacheSerializer {
let scale = (options ?? KingfisherEmptyOptionsInfo).scaleFactor
let preloadAllGIFData = (options ?? KingfisherEmptyOptionsInfo).preloadAllGIFData

return Image.kf_image(data: data, scale: scale, preloadAllGIFData: preloadAllGIFData)
return Kingfisher<Image>.image(data: data, scale: scale, preloadAllGIFData: preloadAllGIFData)
}
}
35 changes: 26 additions & 9 deletions Sources/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ extension CIImageProcessor {
public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
switch item {
case .image(let image):
return image.kf_apply(filter)
return image.kf.apply(filter)
case .data(let data):
return Image.kf_image(data: data, scale: options.scaleFactor, preloadAllGIFData: options.preloadAllGIFData)
return Kingfisher<Image>.image(data: data, scale: options.scaleFactor, preloadAllGIFData: options.preloadAllGIFData)
}
}
}
Expand Down Expand Up @@ -93,36 +93,53 @@ public struct Filter {
}
}

public extension Image {

extension Kingfisher where Base: Image {
/// Apply a `Filter` containing `CIImage` transformer to `self`.
///
/// - parameter filter: The filter used to transform `self`.
///
/// - returns: A transformed image by input `Filter`.
///
/// - Note: Only CG-based images are supported. If any error happens during transforming, `self` will be returned.
public func kf_apply(_ filter: Filter) -> Image {
public func apply(_ filter: Filter) -> Image {

guard let cgImage = cgImage else {
assertionFailure("[Kingfisher] Tint image only works for CG-based image.")
return self
return base
}

let inputImage = CIImage(cgImage: cgImage)
guard let outputImage = filter.transform(inputImage) else {
return self
return base
}

guard let result = ciContext.createCGImage(outputImage, from: outputImage.extent) else {
assertionFailure("[Kingfisher] Can not make an tint image within context.")
return self
return base
}

#if os(macOS)
return kf_fixedForRetinaPixel(cgImage: result, to: kf_size)
return fixedForRetinaPixel(cgImage: result, to: size)
#else
return Image(cgImage: result)
#endif
}

}

public extension Image {

/// Apply a `Filter` containing `CIImage` transformer to `self`.
///
/// - parameter filter: The filter used to transform `self`.
///
/// - returns: A transformed image by input `Filter`.
///
/// - Note: Only CG-based images are supported. If any error happens during transforming, `self` will be returned.
@available(*, deprecated,
message: "Extensions directly on Image are deprecated. Use `kf.apply` instead.",
renamed: "kf.apply")
public func kf_apply(_ filter: Filter) -> Image {
return kf.apply(filter)
}
}
Loading