1.建立一個Project, 先在Info.plist ->右鍵 Open AS -> 選Source Code
在dict內加入,
NSAppTransportSecurity
NSAllowsArbitraryLoads
2.接著建立MyTableViewCell.swift, 後面在Cell建立Image View後拉過來對應
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var iv: UIImageView!
var ivUrl: NSURL! //圖片來源url
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
3.然後在Main.storyboard拉一個Table View
在cell裡面加個Image View, 對應Class: MyTableViewCell
Table View拉一下dataSource與delegate到ViewController記得給Cell的Identifier,這裡寫ImageCell
4.建立一個CustomImageCache.swift檔案
import UIKit
class CustomImageCache {
static let sharedCache: NSCache = { () -> NSCache<AnyObject, AnyObject> in
let cache = NSCache<AnyObject, AnyObject>()
cache.name = "MyImageCache"
cache.countLimit = 20 // Max 20 images in memory.
cache.totalCostLimit = 10*1024*1024 // Max 10MB used.
return cache
}()
}
extension NSURL {
typealias ImageCacheCompletion = (UIImage) -> Void
var cachedImage: UIImage? {
return CustomImageCache.sharedCache.object(
forKey: absoluteString as AnyObject) as? UIImage
}
func fetchImage(completion: @escaping ImageCacheCompletion) {
// 如果需要客製化取得資料在此做
let task = URLSession.shared.dataTask(with: self as URL) {
data, response, error in
if error == nil {
if let data = data, let image = UIImage(data: data) {
CustomImageCache.sharedCache.setObject(
image,
forKey: self.absoluteString as AnyObject,
cost: data.count)
DispatchQueue.main.async() {
completion(image)
}
}
}
}
task.resume()
}
}
5.最後在ViewController.swiftimport UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var url = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// 重複50張圖片
url = Array(repeating: "http://g03.a.alicdn.com/kf/HTB1hvNUIFXXXXbBXFXXq6xXFXXX2/3D-Diamond-Embroidery-Paintings-Rhinestone-Pasted-diy-Diamond-painting-cross-Stitch-font-b-coffe-b-font.jpg", count: 50)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return url.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: MyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! MyTableViewCell
let ivUrl = NSURL(string: url[indexPath.row])
cell.ivUrl = ivUrl // For recycled cells' late image loads.
if let image = ivUrl?.cachedImage { //抓過了 -> 直接顯示
cell.iv.image = image
cell.iv.alpha = 1
} else { //沒抓過 ->下載圖片
cell.iv.alpha = 0
// 下載圖片
ivUrl?.fetchImage { image in
// Check the cell hasn't recycled while loading.
if cell.ivUrl == ivUrl {
cell.iv.image = image
UIView.animate(withDuration: 0.3) {
cell.iv.alpha = 1
}
}
}
}
return cell
}
}
檔案下載:
https://github.com/terryyamg/CacheImag
參考來源:
http://www.splinter.com.au/2015/09/24/swift-image-cache/





Wow back in the day you were pretty aggressive lol. Good post!
回覆刪除ไฮโล
ivip9 พวกเราให้บริการหนังสือกีฬาชั้นหนึ่งสำหรับเกมทุกจำพวก pgslot ผลที่ชนะที่มีให้ในเว็บแห่งนี้ลุ้นระทึกกับเกมของพวกเราบันเทิงใจกับเกมสดจากผู้ให้บริการเกมที่เหมาะสมที่สุด
回覆刪除ufabet company เป็นแพลตฟอร์มการเดิมพันออนไลน์ที่มีชื่อเสียงในประเทศไทย ที่ได้รับความนิยมอย่างแพร่หลายในช่วงหลายปีที่ผ่านมา PG SLOT ในบทความนี้เราจะศึกษาลึกลงไปในโลก
回覆刪除