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

Update for new generic typealias #31

Merged
merged 1 commit into from
Sep 30, 2019
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
20 changes: 10 additions & 10 deletions KingfisherWebP/Classes/Image+WebP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Kingfisher
import CoreGraphics

// MARK: - Image Representation
extension KingfisherWrapper where Base: Image {
extension KingfisherWrapper where Base: KFCrossPlatformImage {
public func webpRepresentation() -> Data? {
if let result = animatedWebPRepresentation() {
return result
Expand All @@ -20,7 +20,7 @@ extension KingfisherWrapper where Base: Image {
}
return nil
}

private func animatedWebPRepresentation() -> Data? {
#if swift(>=4.1)
guard let images = base.images?.compactMap({ $0.cgImage }) else {
Expand All @@ -38,31 +38,31 @@ extension KingfisherWrapper where Base: Image {
}

// MARK: - Create image from WebP data
extension KingfisherWrapper where Base: Image {
static func image(webpData: Data, scale: CGFloat, onlyFirstFrame: Bool) -> Image? {
extension KingfisherWrapper where Base: KFCrossPlatformImage {
static func image(webpData: Data, scale: CGFloat, onlyFirstFrame: Bool) -> KFCrossPlatformImage? {
let frameCount = WebPImageFrameCountGetFromData(webpData as CFData)
if (frameCount == 0) {
return nil
}

if (frameCount == 1 || onlyFirstFrame) {
guard let cgImage = WebPImageCreateWithData(webpData as CFData) else {
return nil
}
return Image(cgImage: cgImage, scale: scale, orientation: .up)
return KFCrossPlatformImage(cgImage: cgImage, scale: scale, orientation: .up)
}

// MARK: Animated images
guard let animationInfo = WebPAnimatedImageInfoCreateWithData(webpData as CFData) as Dictionary? else {
return nil
}
guard let cgFrames = animationInfo[kWebPAnimatedImageFrames] as? [CGImage] else {
return nil
}
let uiFrames = cgFrames.map { Image(cgImage: $0, scale: scale, orientation: .up) }
let uiFrames = cgFrames.map { KFCrossPlatformImage(cgImage: $0, scale: scale, orientation: .up) }

let duration = (animationInfo[kWebPAnimatedImageDuration] as? NSNumber).flatMap { $0.doubleValue as TimeInterval } ?? 0.1 * TimeInterval(frameCount)
return Image.animatedImage(with: uiFrames, duration: duration)
return KFCrossPlatformImage.animatedImage(with: uiFrames, duration: duration)
}
}

Expand Down
4 changes: 2 additions & 2 deletions KingfisherWebP/Classes/WebPProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public struct WebPProcessor: ImageProcessor {

public init() {}

public func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> Image? {
public func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
switch item {
case .image(let image):
return image
case .data(let data):
if data.isWebPFormat {
return KingfisherWrapper<Image>.image(webpData: data, scale: options.scaleFactor, onlyFirstFrame: options.onlyLoadFirstFrame)
return KingfisherWrapper<KFCrossPlatformImage>.image(webpData: data, scale: options.scaleFactor, onlyFirstFrame: options.onlyLoadFirstFrame)
} else {
return DefaultImageProcessor.default.process(item: item, options: options)
}
Expand Down
4 changes: 2 additions & 2 deletions KingfisherWebP/Classes/WebPSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public struct WebPSerializer: CacheSerializer {
public static let `default` = WebPSerializer()
private init() {}

public func data(with image: Image, original: Data?) -> Data? {
public func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
if let original = original, !original.isWebPFormat {
return DefaultCacheSerializer.default.data(with: image, original: original)
} else {
return image.kf.normalized.kf.webpRepresentation()
}
}

public func image(with data: Data, options: KingfisherParsedOptionsInfo) -> Image? {
public func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
return WebPProcessor.default.process(item: .data(data), options: options)
}
}