Browse Source

Update tests for OSX GIF support

onevcat 10 years ago
parent
commit
b0e97995db

+ 1 - 0
Kingfisher-Demo/ViewController.swift

@@ -17,6 +17,7 @@
 //  all copies or substantial portions of the Software.
 //
 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+@IBOutlet weak var imageView: NSImageView!
 //  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

+ 15 - 5
Kingfisher-OSX-Demo/Base.lproj/Main.storyboard

@@ -1,7 +1,8 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="6198" systemVersion="14A297b" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
+<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
     <dependencies>
-        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6198"/>
+        <deployment identifier="macosx"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="9531"/>
     </dependencies>
     <scenes>
         <!--Application-->
@@ -641,12 +642,12 @@
                         <outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
                     </connections>
                 </application>
-                <customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModuleProvider="target"/>
+                <customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Kingfisher_OSX_Demo" customModuleProvider="target"/>
                 <customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
             </objects>
             <point key="canvasLocation" x="75" y="0.0"/>
         </scene>
-        <!--Window Controller - Window-->
+        <!--Window Controller-->
         <scene sceneID="R2V-B0-nI4">
             <objects>
                 <windowController id="B8D-0N-5wS" sceneMemberID="viewController">
@@ -667,11 +668,20 @@
         <!--View Controller-->
         <scene sceneID="hIz-AP-VOD">
             <objects>
-                <viewController id="XfG-lQ-9wD" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
+                <viewController id="XfG-lQ-9wD" customClass="ViewController" customModule="Kingfisher_OSX_Demo" customModuleProvider="target" sceneMemberID="viewController">
                     <view key="view" id="m2S-Jp-Qdl">
                         <rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
                         <autoresizingMask key="autoresizingMask"/>
+                        <subviews>
+                            <imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="49l-VB-b2Y">
+                                <rect key="frame" x="53" y="0.0" width="227" height="270"/>
+                                <imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" id="1n5-6g-sDy"/>
+                            </imageView>
+                        </subviews>
                     </view>
+                    <connections>
+                        <outlet property="imageView" destination="49l-VB-b2Y" id="A9c-YP-MUH"/>
+                    </connections>
                 </viewController>
                 <customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
             </objects>

+ 15 - 1
Kingfisher-OSX-Demo/ViewController.swift

@@ -6,14 +6,28 @@
 //  Copyright © 2016年 Wei Wang. All rights reserved.
 //
 
-import Cocoa
+import AppKit
 
 class ViewController: NSViewController {
 
+    
+    @IBOutlet weak var imageView: NSImageView!
+    
     override func viewDidLoad() {
         super.viewDidLoad()
 
         // Do any additional setup after loading the view.
+        imageView.imageScaling = .ScaleNone
+        imageView.animates = true
+        
+        let image = NSImage(named: "dancing-banana.gif")
+        
+        let ddd = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("dancing-banana", ofType: "gif")!)!
+        
+        let rep = image!.representations.first as! NSBitmapImageRep
+        print(rep.valueForProperty(NSImageFrameCount))
+        let data = rep.representationUsingType(.NSGIFFileType, properties: [:])!
+        imageView.image = NSImage(data: data)
     }
 
     override var representedObject: AnyObject? {

+ 11 - 0
Kingfisher.xcodeproj/project.pbxproj

@@ -39,6 +39,11 @@
 		4BBA04C41AD7986100A5CF82 /* KingfisherTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BBA04C31AD7986100A5CF82 /* KingfisherTestHelper.swift */; };
 		D10EC2341C3D632300A4211C /* KingfisherTests_OSX.swift in Sources */ = {isa = PBXBuildFile; fileRef = D10EC2331C3D632300A4211C /* KingfisherTests_OSX.swift */; };
 		D10EC2361C3D632300A4211C /* Kingfisher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B2944481C3D01B20088C3E7 /* Kingfisher.framework */; };
+		D10EC23C1C3D64D200A4211C /* ImageExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1A7B6C91BDBC9C60083A684 /* ImageExtensionTests.swift */; };
+		D10EC23D1C3D653E00A4211C /* KingfisherTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BBA04C31AD7986100A5CF82 /* KingfisherTestHelper.swift */; };
+		D10EC23E1C3D659300A4211C /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B29446C1C3D05FD0088C3E7 /* Image.swift */; };
+		D10EC2411C3D6EEC00A4211C /* dancing-banana.gif in Resources */ = {isa = PBXBuildFile; fileRef = D1A7B6CB1BDBEC090083A684 /* dancing-banana.gif */; };
+		D10EC2421C3D75BF00A4211C /* dancing-banana.gif in Resources */ = {isa = PBXBuildFile; fileRef = D1A7B6CB1BDBEC090083A684 /* dancing-banana.gif */; };
 		D11250C11BAC4B6300B986EF /* ImageTransition.swift in Sources */ = {isa = PBXBuildFile; fileRef = D11250C01BAC4B6300B986EF /* ImageTransition.swift */; };
 		D13F49C51BEDA53F00CE335D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D13F49C41BEDA53F00CE335D /* AppDelegate.swift */; };
 		D13F49CA1BEDA53F00CE335D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D13F49C81BEDA53F00CE335D /* Main.storyboard */; };
@@ -740,6 +745,7 @@
 			files = (
 				4B29445C1C3D03880088C3E7 /* Assets.xcassets in Resources */,
 				4B29445F1C3D03880088C3E7 /* Main.storyboard in Resources */,
+				D10EC2411C3D6EEC00A4211C /* dancing-banana.gif in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -747,6 +753,7 @@
 			isa = PBXResourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				D10EC2421C3D75BF00A4211C /* dancing-banana.gif in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -870,6 +877,9 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				D10EC23E1C3D659300A4211C /* Image.swift in Sources */,
+				D10EC23D1C3D653E00A4211C /* KingfisherTestHelper.swift in Sources */,
+				D10EC23C1C3D64D200A4211C /* ImageExtensionTests.swift in Sources */,
 				D10EC2341C3D632300A4211C /* KingfisherTests_OSX.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
@@ -1428,6 +1438,7 @@
 				D10EC23B1C3D632300A4211C /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 		D13F49D01BEDA53F00CE335D /* Build configuration list for PBXNativeTarget "Kingfisher-tvOS-Demo" */ = {
 			isa = XCConfigurationList;

+ 34 - 10
Kingfisher/Image.swift

@@ -17,6 +17,9 @@ public typealias Image = UIImage
 
 import ImageIO
 
+private var imagesKey: Void?
+private var durationKey: Void?
+
 extension Image {
 #if os(OSX)
     
@@ -28,12 +31,22 @@ extension Image {
         return 1.0
     }
     
-    var kf_images: [Image]? {
-        return nil
+    private(set) var kf_images: [Image]? {
+        get {
+            return objc_getAssociatedObject(self, &imagesKey) as? [Image]
+        }
+        set {
+            objc_setAssociatedObject(self, &imagesKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
+        }
     }
     
-    var kf_duration: NSTimeInterval {
-        return 0
+    private(set) var kf_duration: NSTimeInterval {
+        get {
+            return objc_getAssociatedObject(self, &durationKey) as? NSTimeInterval ?? 0.0
+        }
+        set {
+            objc_setAssociatedObject(self, &durationKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
+        }
     }
 
     static func kf_imageWithCGImage(cgImage: CGImageRef, scale: CGFloat, refImage: Image?) -> Image {
@@ -62,12 +75,11 @@ extension Image {
     }
     
     static func kf_imageWithCGImage(cgImage: CGImageRef, scale: CGFloat, refImage: Image?) -> Image {
-    if let refImage = refImage {
-        return Image(CGImage: cgImage, scale: scale, orientation: refImage.imageOrientation)
-    } else {
-        return Image(CGImage: cgImage, scale: scale, orientation: .Up)
-    }
-    
+        if let refImage = refImage {
+            return Image(CGImage: cgImage, scale: scale, orientation: refImage.imageOrientation)
+        } else {
+            return Image(CGImage: cgImage, scale: scale, orientation: .Up)
+        }    
     }
     
     public func kf_normalizedImage() -> Image {
@@ -177,11 +189,23 @@ extension Image {
             images.append(Image.kf_imageWithCGImage(imageRef, scale: scale, refImage: nil))
         }
         
+#if os(OSX)
+        if let image = Image(data: data) {
+            image.kf_images = images
+            image.kf_duration = gifDuration
+            return image
+        }
+        return nil
+#else
         if frameCount == 1 {
             return images.first
         } else {
             return Image.kf_animatedImageWithImages(images, duration: duration <= 0.0 ? gifDuration : duration)
         }
+#endif
+        
+
+        
     }
 }
 

+ 7 - 7
KingfisherTests/ImageExtensionTests.swift

@@ -38,22 +38,22 @@ class ImageExtensionTests: XCTestCase {
     }
     
     func testGenerateGIFImage() {
-        let image = UIImage.kf_animatedImageWithGIFData(gifData: testImageGIFData)
+        let image = Image.kf_animatedImageWithGIFData(gifData: testImageGIFData)
         XCTAssertNotNil(image, "The image should be initiated.")
-        XCTAssertEqual(image!.images!.count, 8, "There should be 8 frames.")
+        XCTAssertEqual(image!.kf_images!.count, 8, "There should be 8 frames.")
         
-        XCTAssertEqualWithAccuracy(image!.duration, 0.8, accuracy: 0.001, "The image duration should be 0.8s")
+        XCTAssertEqualWithAccuracy(image!.kf_duration, 0.8, accuracy: 0.001, "The image duration should be 0.8s")
     }
     
     func testGIFRepresentation() {
-        let image = UIImage.kf_animatedImageWithGIFData(gifData: testImageGIFData)!
+        let image = Image.kf_animatedImageWithGIFData(gifData: testImageGIFData)!
         let data = ImageGIFRepresentation(image)
         
         XCTAssertNotNil(data, "Data should not be nil")
         XCTAssertEqual(data?.kf_imageFormat, ImageFormat.GIF)
         
-        let image1 = UIImage.kf_animatedImageWithGIFData(gifData: data!)!
-        XCTAssertEqual(image1.duration, image.duration)
-        XCTAssertEqual(image1.images!.count, image.images!.count)
+        let image1 = Image.kf_animatedImageWithGIFData(gifData: data!)!
+        XCTAssertEqual(image1.kf_duration, image.kf_duration)
+        XCTAssertEqual(image1.kf_images!.count, image.kf_images!.count)
     }
 }

File diff suppressed because it is too large
+ 4 - 1
KingfisherTests/KingfisherTestHelper.swift


Some files were not shown because too many files changed in this diff