Browse Source

Fix callback. Update demo

onevcat 11 years ago
parent
commit
06a66b0318

+ 4 - 0
Kingfisher-Demo.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		4BAFBA3D1AD671E400FB0300 /* CollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BAFBA3C1AD671E400FB0300 /* CollectionViewCell.swift */; };
 		D151E72B1AD3C48D004FD4AE /* UIImage+Decode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D151E72A1AD3C48D004FD4AE /* UIImage+Decode.swift */; };
 		D1ED2D111AD2CFA600CFC3EB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D101AD2CFA600CFC3EB /* AppDelegate.swift */; };
 		D1ED2D131AD2CFA600CFC3EB /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D121AD2CFA600CFC3EB /* ViewController.swift */; };
@@ -65,6 +66,7 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
+		4BAFBA3C1AD671E400FB0300 /* CollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCell.swift; sourceTree = "<group>"; };
 		D151E72A1AD3C48D004FD4AE /* UIImage+Decode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Decode.swift"; sourceTree = "<group>"; };
 		D1ED2D0B1AD2CFA600CFC3EB /* Kingfisher-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Kingfisher-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
 		D1ED2D0F1AD2CFA600CFC3EB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -137,6 +139,7 @@
 		D1ED2D0D1AD2CFA600CFC3EB /* Kingfisher-Demo */ = {
 			isa = PBXGroup;
 			children = (
+				4BAFBA3C1AD671E400FB0300 /* CollectionViewCell.swift */,
 				D1ED2D101AD2CFA600CFC3EB /* AppDelegate.swift */,
 				D1ED2D121AD2CFA600CFC3EB /* ViewController.swift */,
 				D1ED2D141AD2CFA600CFC3EB /* Main.storyboard */,
@@ -341,6 +344,7 @@
 			files = (
 				D1ED2D131AD2CFA600CFC3EB /* ViewController.swift in Sources */,
 				D1ED2D111AD2CFA600CFC3EB /* AppDelegate.swift in Sources */,
+				4BAFBA3D1AD671E400FB0300 /* CollectionViewCell.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 74 - 19
Kingfisher-Demo/Base.lproj/Main.storyboard

@@ -1,33 +1,88 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6751" systemVersion="14C1514" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7531" systemVersion="14D131" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="peg-r0-mlo">
     <dependencies>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6736"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/>
     </dependencies>
     <scenes>
         <!--View Controller-->
-        <scene sceneID="tne-QT-ifu">
+        <scene sceneID="q9F-GD-CcN">
             <objects>
-                <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Kingfisher_Demo" customModuleProvider="target" sceneMemberID="viewController">
-                    <layoutGuides>
-                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
-                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
-                    </layoutGuides>
-                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
+                <collectionViewController id="IdS-po-MDe" customClass="ViewController" customModule="Kingfisher_Demo" customModuleProvider="target" sceneMemberID="viewController">
+                    <collectionView key="view" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" id="cQA-0g-Ig3">
                         <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
-                        <subviews>
-                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="xN4-kw-a8U">
-                                <rect key="frame" x="0.0" y="0.0" width="221" height="197"/>
-                            </imageView>
-                        </subviews>
-                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
-                    </view>
+                        <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+                        <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="46n-QC-xKN">
+                            <size key="itemSize" width="250" height="250"/>
+                            <size key="headerReferenceSize" width="0.0" height="0.0"/>
+                            <size key="footerReferenceSize" width="0.0" height="0.0"/>
+                            <inset key="sectionInset" minX="0.0" minY="10" maxX="0.0" maxY="10"/>
+                        </collectionViewFlowLayout>
+                        <cells>
+                            <collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="collectionViewCell" id="jYH-ix-b6K" customClass="CollectionViewCell" customModule="Kingfisher_Demo" customModuleProvider="target">
+                                <rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
+                                <autoresizingMask key="autoresizingMask"/>
+                                <view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
+                                    <rect key="frame" x="0.0" y="0.0" width="250" height="250"/>
+                                    <autoresizingMask key="autoresizingMask"/>
+                                    <subviews>
+                                        <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="5wL-TX-hML">
+                                            <rect key="frame" x="0.0" y="0.0" width="250" height="250"/>
+                                        </imageView>
+                                    </subviews>
+                                    <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
+                                </view>
+                                <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+                                <constraints>
+                                    <constraint firstAttribute="bottom" secondItem="5wL-TX-hML" secondAttribute="bottom" id="6Fx-JY-GDB"/>
+                                    <constraint firstAttribute="trailing" secondItem="5wL-TX-hML" secondAttribute="trailing" id="Fev-Dq-rlR"/>
+                                    <constraint firstItem="5wL-TX-hML" firstAttribute="top" secondItem="jYH-ix-b6K" secondAttribute="top" id="e9v-9B-3Ji"/>
+                                    <constraint firstItem="5wL-TX-hML" firstAttribute="leading" secondItem="jYH-ix-b6K" secondAttribute="leading" id="rxf-rn-zPF"/>
+                                </constraints>
+                                <connections>
+                                    <outlet property="cellImageView" destination="5wL-TX-hML" id="vlC-e9-WbX"/>
+                                </connections>
+                            </collectionViewCell>
+                        </cells>
+                        <connections>
+                            <outlet property="dataSource" destination="IdS-po-MDe" id="FRQ-Vg-rwJ"/>
+                            <outlet property="delegate" destination="IdS-po-MDe" id="VdT-PX-8WU"/>
+                        </connections>
+                    </collectionView>
+                    <navigationItem key="navigationItem" id="tBW-1C-FRK">
+                        <barButtonItem key="leftBarButtonItem" title="Clear Cache" id="5vP-RF-3ee">
+                            <connections>
+                                <action selector="clearCache:" destination="IdS-po-MDe" id="sjm-lk-eGI"/>
+                            </connections>
+                        </barButtonItem>
+                        <barButtonItem key="rightBarButtonItem" title="Reload" id="FbL-Yi-uUn">
+                            <connections>
+                                <action selector="reload:" destination="IdS-po-MDe" id="fTQ-Jo-Q55"/>
+                            </connections>
+                        </barButtonItem>
+                    </navigationItem>
+                </collectionViewController>
+                <placeholder placeholderIdentifier="IBFirstResponder" id="Djx-Kg-Zzu" userLabel="First Responder" sceneMemberID="firstResponder"/>
+            </objects>
+            <point key="canvasLocation" x="1107" y="647"/>
+        </scene>
+        <!--Navigation Controller-->
+        <scene sceneID="a89-ss-78Y">
+            <objects>
+                <navigationController automaticallyAdjustsScrollViewInsets="NO" id="peg-r0-mlo" sceneMemberID="viewController">
+                    <toolbarItems/>
+                    <navigationBar key="navigationBar" contentMode="scaleToFill" id="ese-eO-cur">
+                        <rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
+                        <autoresizingMask key="autoresizingMask"/>
+                    </navigationBar>
+                    <nil name="viewControllers"/>
                     <connections>
-                        <outlet property="imageView" destination="xN4-kw-a8U" id="p9e-iJ-uYa"/>
+                        <segue destination="IdS-po-MDe" kind="relationship" relationship="rootViewController" id="Mf8-3g-VXm"/>
                     </connections>
-                </viewController>
-                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
+                </navigationController>
+                <placeholder placeholderIdentifier="IBFirstResponder" id="Ull-Ta-ybR" userLabel="First Responder" sceneMemberID="firstResponder"/>
             </objects>
+            <point key="canvasLocation" x="295" y="647"/>
         </scene>
     </scenes>
 </document>

+ 14 - 0
Kingfisher-Demo/CollectionViewCell.swift

@@ -0,0 +1,14 @@
+//
+//  CollectionViewCell.swift
+//  Kingfisher-Demo
+//
+//  Created by WANG WEI on 2015/04/09.
+//  Copyright (c) 2015年 Wei Wang. All rights reserved.
+//
+
+import UIKit
+
+class CollectionViewCell: UICollectionViewCell {
+    
+    @IBOutlet weak var cellImageView: UIImageView!
+}

+ 26 - 5
Kingfisher-Demo/ViewController.swift

@@ -9,14 +9,12 @@
 import UIKit
 import Kingfisher
 
-class ViewController: UIViewController {
+class ViewController: UICollectionViewController {
 
-    @IBOutlet weak var imageView: UIImageView!
     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.
-        imageView.kf_setImageWithURL(NSURL(string: "http://onevcat.com/content/images/2014/May/200.jpg")!, placeHolderImage: nil, options: KingfisherOptions.LowPriority | KingfisherOptions.BackgroundDecode)
-        
+        title = "Kingfisher"
     }
 
     override func didReceiveMemoryWarning() {
@@ -24,6 +22,29 @@ class ViewController: UIViewController {
         // Dispose of any resources that can be recreated.
     }
 
-
+    @IBAction func clearCache(sender: AnyObject) {
+        KingfisherManager.sharedManager.cache.clearMemoryCache()
+        KingfisherManager.sharedManager.cache.clearDiskCache()
+    }
+    
+    @IBAction func reload(sender: AnyObject) {
+        collectionView?.reloadData()
+    }
 }
 
+extension ViewController: UICollectionViewDataSource {
+    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
+        return 10
+    }
+    
+    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
+        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
+        cell.cellImageView.kf_setImageWithURL(NSURL(string: "http://lorempixel.com/250/250/cats/\(indexPath.row + 1)/")!, placeHolderImage: nil, options: KingfisherOptions.LowPriority, progressBlock: { (receivedSize, totalSize) -> () in
+            println("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
+        }) { (image, error, imageURL) -> () in
+            println("\(indexPath.row + 1): Finished")
+        }
+        
+        return cell
+    }
+}

+ 4 - 6
Kingfisher/ImageDownloader.swift

@@ -120,13 +120,11 @@ extension ImageDownloader: NSURLSessionDataDelegate {
     }
     
     private func callbackWithImage(image: UIImage?, error: NSError?, imageURL: NSURL) {
-        dispatch_async(dispatch_get_main_queue(), { () -> Void in
-            if let callbackPairs = self.fetchLoads[imageURL]?.callbacks {
-                for callbackPair in callbackPairs {
-                    callbackPair.completionHander?(image: image, error: error, imageURL: imageURL)
-                }
+        if let callbackPairs = self.fetchLoads[imageURL]?.callbacks {
+            for callbackPair in callbackPairs {
+                callbackPair.completionHander?(image: image, error: error, imageURL: imageURL)
             }
-        })
+        }
     }
     
     public func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {

+ 1 - 3
Kingfisher/UIImageView+Kingfisher.swift

@@ -45,9 +45,7 @@ public extension UIImageView {
                          progressBlock: DownloadProgressBlock?,
                      completionHandler: CompletionHandler?) -> RetrieveImageTask
     {
-        if let placeHolderImage = placeHolderImage {
-            image = placeHolderImage
-        }
+        image = placeHolderImage
         
         self.kf_setWebUrl(url)
         let task = KingfisherManager.sharedManager.retriveImageWithURL(url, options: options, progressBlock: { (recivedSize, totalSize) -> () in