Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from systembugtj/master
Browse files Browse the repository at this point in the history
allow datadecoder can be used outside file. Check if it's webp format.
  • Loading branch information
ryokosuge authored Feb 14, 2018
2 parents 3a95640 + dcd0520 commit 77f69a8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Nuke-WebP-Plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
C7A377CC20334B7500339789 /* sample.gif in Resources */ = {isa = PBXBuildFile; fileRef = C74750E12030E2FC00FA7FE2 /* sample.gif */; };
F80A457F20197CE200825C6D /* WebPDecodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F80A457E20197CE200825C6D /* WebPDecodeTests.swift */; };
F80A458120197DB600825C6D /* LoadWebPImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F80A458020197DB600825C6D /* LoadWebPImageTests.swift */; };
F814657E200EE40500364146 /* Nuke_WebP_Plugin.h in Headers */ = {isa = PBXBuildFile; fileRef = F814657C200EE40500364146 /* Nuke_WebP_Plugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -186,6 +187,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C74750E12030E2FC00FA7FE2 /* sample.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = sample.gif; sourceTree = "<group>"; };
F80A457E20197CE200825C6D /* WebPDecodeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebPDecodeTests.swift; sourceTree = "<group>"; };
F80A458020197DB600825C6D /* LoadWebPImageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadWebPImageTests.swift; sourceTree = "<group>"; };
F8146577200EE3C700364146 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -433,6 +435,7 @@
F8FAA4D2200EF07300DD0BED /* Resource */ = {
isa = PBXGroup;
children = (
C74750E12030E2FC00FA7FE2 /* sample.gif */,
F8FAA4D3200EF08400DD0BED /* sample.webp */,
);
path = Resource;
Expand Down Expand Up @@ -831,6 +834,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C7A377CC20334B7500339789 /* sample.gif in Resources */,
F8FAA4D4200EF08400DD0BED /* sample.webp in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file added Nuke-WebP-PluginTests/Resource/sample.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion Nuke-WebP-PluginTests/WebPDecodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class WebPDecodeTests: XCTestCase {
return webpImagePath
}()

private lazy var gifImagePath: URL = {
let gifImagePath = Bundle(for: type(of: self)).url(forResource: "sample", withExtension: "gif")!
return gifImagePath
}()

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
Expand All @@ -26,7 +31,7 @@ class WebPDecodeTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testsDecodeWebPImage() {
do {
let webpData = try Data(contentsOf: self.webpImagePath)
Expand All @@ -39,6 +44,19 @@ class WebPDecodeTests: XCTestCase {
XCTFail(e.localizedDescription)
}
}

func testsDecodeNotWebPImage() {
do {
let gifData = try Data(contentsOf: self.gifImagePath)
let image: UIImage? = UIImage(data: gifData)
XCTAssertNotNil(image)

let webpImage: Image? = NukeWebPPlugin.WebPImageDecoder.decode(gifData)
XCTAssertNil(webpImage)
} catch let e {
XCTFail(e.localizedDescription)
}
}

func testPerformanceDecodeWebP() {
// This is an example of a performance test case.
Expand Down
29 changes: 27 additions & 2 deletions Source/WebPImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public extension WebPImage {
public static let manager: Nuke.Manager = {
let dataDecoder = Nuke.DataDecoder()
let webpDataDecoder = WebPDataDecoder()
let decoder = Nuke.DataDecoderComposition(decoders: [dataDecoder, webpDataDecoder])
let decoder = Nuke.DataDecoderComposition(decoders: [webpDataDecoder, dataDecoder])
let loader = Nuke.Loader(loader: DataLoader(), decoder: decoder)
return Nuke.Manager(loader: loader)
}()
Expand All @@ -30,11 +30,36 @@ public struct WebPDataDecoder: Nuke.DataDecoding {
}

public func decode(data: Data, response: URLResponse) -> Image? {
if !data.isWebPFormat {
return nil
}
return decodeWebPData(data)
}

internal func decodeWebPData(_ webpData: Data) -> Image? {
return WebPImageDecoder.decode(webpData)
}
}

// MARK: - WebP Format Testing
extension Data {
// Borrow from KingfisherWebp
// https://github.com/Yeatse/KingfisherWebP/blob/master/KingfisherWebP/Classes/Image%2BWebP.swift#L38
var isWebPFormat: Bool {
if count < 12 {
return false
}

let endIndex = index(startIndex, offsetBy: 12)
let testData = subdata(in: startIndex..<endIndex)
guard let testString = String(data: testData, encoding: .ascii) else {
return false
}

if testString.hasPrefix("RIFF") && testString.hasSuffix("WEBP") {
return true
} else {
return false
}
}
}

0 comments on commit 77f69a8

Please sign in to comment.