[快速学会Swift第三方库] HanekeSwift篇
Haneke是一个轻量级的缓存,为UIImage,JSON,NSData,String提供记忆和LRU磁盘缓存。
目录
编码之前
编码之前
导入HanekeSwift
推荐使用CocoaPods进行导入,CocoaPods是一个负责管理iOS项目中第三方开源库的工具,安装CocoaPods之后使用命令行就能轻松地对所有第三方开源库进行安装和更新,而不需要每次上GitHub去下载。
CocoaPods的安装过程传送门:iOS 9 导入类库全面详尽过程(Ruby安装->CocoaPods安装->导入类库)
手动下载:GitHub-Haneke主页
装好CocoaPods后,修改Podfile文件内容为如下:
source 'https://github.com/CocoaPods/Specs.git'@H_301_77@
platform@H_301_77@ :ios,'9.0'@H_301_77@
use_frameworks!
target 'Web'@H_301_77@ do@H_301_77@
pod 'HanekeSwift'@H_301_77@
end@H_301_77@@H_301_77@
xcodeproj 'Desktop/Web/Web.xcodeproj'@H_301_77@
target后面为工程名,最后一行为工程路径(这里的Web是我的工程名)
再执行命令:
$ @H_301_77@pod install
其他操作
另外还需要在Target->工程名->Build Settings->Search Paths->User Header Search Paths处添加HanekeSwift所在的目录:
最后在你需要用到HanekeSwift的类中加上:
import@H_301_77@ Haneke
使用缓存
NSData
示例代码
let@H_301_77@ url =@H_301_77@ NSURL(string@H_301_77@:"http://blog.csdn.net/sps900608"@H_301_77@)!@H_301_77@
let@H_301_77@ data@H_301_77@ =@H_301_77@ NSData(contentsOfURL: url)!@H_301_77@
let@H_301_77@ cache@H_301_77@ =@H_301_77@ Shared.@H_301_77@dataCache
//存入缓存@H_301_77@
cache@H_301_77@.@H_301_77@set@H_301_77@(value: data@H_301_77@,key: "data"@H_301_77@)
//获取缓存@H_301_77@
cache@H_301_77@.@H_301_77@fetch(key: "data"@H_301_77@) { (cacheData) in@H_301_77@
self@H_301_77@.@H_301_77@webView.@H_301_77@loadData(cacheData,MIMEType: "text/html"@H_301_77@,textEncodingName: "utf-8"@H_301_77@,baseURL: NSURL())
}
运行结果
JSON
更多关于JSON的用法,可以参考
[快速学会Swift第三方库] SwiftyJSON篇
Swift学习笔记(2)网络数据交换格式(XML,JSON)解析
测试接口
在浏览器中打开”https://api.github.com/users/haneke“可以看到:
{
"login@H_301_77@": "Haneke"@H_301_77@@H_301_77@,"id@H_301_77@": 8600207@H_301_77@@H_301_77@,"avatar_url@H_301_77@": "https://avatars.githubusercontent.com/u/8600207?v=3"@H_301_77@@H_301_77@,"gravatar_id@H_301_77@": ""@H_301_77@@H_301_77@,"url@H_301_77@": "https://api.github.com/users/Haneke"@H_301_77@@H_301_77@,"html_url@H_301_77@": "https://github.com/Haneke"@H_301_77@@H_301_77@,"followers_url@H_301_77@": "https://api.github.com/users/Haneke/followers"@H_301_77@@H_301_77@,"following_url@H_301_77@": "https://api.github.com/users/Haneke/following{/other_user}"@H_301_77@@H_301_77@,"gists_url@H_301_77@": "https://api.github.com/users/Haneke/gists{/gist_id}"@H_301_77@@H_301_77@,"starred_url@H_301_77@": "https://api.github.com/users/Haneke/starred{/owner}{/repo}"@H_301_77@@H_301_77@,"subscriptions_url@H_301_77@": "https://api.github.com/users/Haneke/subscriptions"@H_301_77@@H_301_77@,"organizations_url@H_301_77@": "https://api.github.com/users/Haneke/orgs"@H_301_77@@H_301_77@,"repos_url@H_301_77@": "https://api.github.com/users/Haneke/repos"@H_301_77@@H_301_77@,"events_url@H_301_77@": "https://api.github.com/users/Haneke/events{/privacy}"@H_301_77@@H_301_77@,"received_events_url@H_301_77@": "https://api.github.com/users/Haneke/received_events"@H_301_77@@H_301_77@,"type@H_301_77@": "Organization"@H_301_77@@H_301_77@,"site_admin@H_301_77@": false@H_301_77@@H_301_77@,"name@H_301_77@": "Haneke"@H_301_77@@H_301_77@,"company@H_301_77@": null@H_301_77@@H_301_77@,"blog@H_301_77@": null@H_301_77@@H_301_77@,"location@H_301_77@": null@H_301_77@@H_301_77@,"email@H_301_77@": null@H_301_77@@H_301_77@,"hireable@H_301_77@": null@H_301_77@@H_301_77@,"bio@H_301_77@": "A lightweight zero-config image cache for iOS,in Swift and Objective-C."@H_301_77@@H_301_77@,"public_repos@H_301_77@": 4@H_301_77@@H_301_77@,"public_gists@H_301_77@": 0@H_301_77@@H_301_77@,"followers@H_301_77@": 0@H_301_77@@H_301_77@,"following@H_301_77@": 0@H_301_77@@H_301_77@,"created_at@H_301_77@": "2014-08-30T17:18:40Z"@H_301_77@@H_301_77@,"updated_at@H_301_77@": "2015-04-27T16:41:09Z"@H_301_77@ @H_301_77@}
示例代码
func jsonCache() {
let@H_301_77@ url =@H_301_77@ NSURL(string@H_301_77@: "https://api.github.com/users/haneke"@H_301_77@)!@H_301_77@
let@H_301_77@ cache@H_301_77@ =@H_301_77@ Shared.@H_301_77@JSONCache
cache@H_301_77@.@H_301_77@fetch(URL: url) { (json) in@H_301_77@
print(json.@H_301_77@dictionary?@H_301_77@@H_400_404@[@H_301_77@"bio"@H_301_77@@H_400_404@]@H_301_77@) } }@H_301_77@
运行结果
Optional(A lightweight zero-config@H_301_77@ image cache@H_301_77@ for iOS,in@H_301_77@ Swift and@H_301_77@ Objective-C@H_301_77@.@H_301_77@)
UIImage和String
与NSData,JSON用法类似,只是定义Cache时有点区别:
let@H_301_77@ cache = Shared@H_301_77@.imageCache
let@H_301_77@ cache = Shared@H_301_77@.stringCache
UIImage的拓展用法
示例代码
func imageCache@H_301_77@@H_301_77@() {
let@H_301_77@ url = NSURL(string: "http://www.51work6.com/service/download.PHP?email=scuxiatian@foxmail.com&FileName=test1.jpg"@H_301_77@)!
imageView.hnk_set@H_301_77@ImageFromURL(url)
}
或者
func imageCache() {
let@H_301_77@ url = NSURL@H_301_77@(string: "http://www.51work6.com/service/download.PHP?email=scuxiatian@foxmail.com&FileName=test1.jpg"@H_301_77@)!
let@H_301_77@ data@H_301_77@ = NSData@H_301_77@(contentsOfURL@H_301_77@: url@H_301_77@)@H_301_77@!@H_301_77@
let@H_301_77@ image = UIImage@H_301_77@(data@H_301_77@: data@H_301_77@)!@H_301_77@
imageView.hnk_setImage(image,key: "test"@H_301_77@)
}
运行结果
fetcher的其他用法
func useFetcher() {
let@H_301_77@ url =@H_301_77@ NSURL(string@H_301_77@: "http://www.51work6.com/service/download.PHP?email=scuxiatian@foxmail.com&FileName=test1.jpg"@H_301_77@)!@H_301_77@
let@H_301_77@ fetcher =@H_301_77@ NetworkFetcher<@H_301_77@UIImage>@H_301_77@(URL: url)
let@H_301_77@ cache@H_301_77@ =@H_301_77@ Shared.@H_301_77@imageCache
cache@H_301_77@.@H_301_77@fetch(fetcher: fetcher).@H_301_77@onSuccess { (image) in@H_301_77@
self@H_301_77@.@H_301_77@imageView.@H_301_77@image =@H_301_77@ image
}
}
运行效果与前面完全相同
深入学习
这里只列出了最常用的几种用法,如果你希望能够更加深入地学习HanekeSwift,可以前往GitHub-Haneke主页!