- This is a simple web image downloader, similar to a simple version of SDWebImage or Kingfisher.
- 這是一個簡單的網路圖片下載工具,類似SDWebImage或Kingfisher的簡單版本。
- 使用WWNetworking + WWSQLite3Manager套件來延伸製作,且利用網路圖片的Header-Tag,Last-Modified / ETag,來實現快取功能…
dependencies: [
.package(url: "https://github.com/William-Weng/WWNetworking-UIImage.git", .upToNextMajor(from: "1.6.0"))
]
函式 | 說明 |
---|---|
initDatabase(for:expiredDays:cacheDelayTime:maxnumDownloadCount:defaultImage:) | 初始化資料庫 |
removeExpiredCacheImages(expiredDays:) | 移除過期圖片 |
downloadImage(with:pixelSize:) | 下載圖片 + 設定最大像素 |
cacheImageData(with:) | 讀取快取圖片資料 |
cacheImage(with:) | 讀取快取圖片 |
downloadProgress(block:) | 圖片下載進度 |
removeExpiredCacheImagesProgress(block:) | 刪除過期圖片進度 |
errorBlock(block:) | 相關錯誤訊息輸出 |
import UIKit
import WWNetworking_UIImage
@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
_ = WWWebImage.shared.initDatabase(for: .documents, expiredDays: 90, cacheDelayTime: 600, maxnumDownloadCount: 10, defaultImage: UIImage(named: "no-pictures"))
return true
}
}
import UIKit
import WWNetworking_UIImage
final class TableViewContrller: UIViewController {
@IBOutlet weak var myTableView: UITableView!
private let imageUrls = [
"https://i.pinimg.com/originals/bd/bd/4f/bdbd4f6d85fdaf6c8eea6ffc99aeaa1a.jpg",
"https://www.niusnews.com/upload/imgs/default/2020Apr_CHOU/0428Sumikko/A1.jpg",
"https://cc.tvbs.com.tw/img/upload/2020/05/19/20200519191617-75e42ad2.jpg",
"https://s.yimg.com/ny/api/res/1.2/MubECS_oug7X7MVtm9v9bg--/YXBwaWQ9aGlnaGxhbmRlcjt3PTY0MA--/https://media.zenfs.com/en/dailyview.tw/26023bd61a23e81bf2c4005d03c881a4",
"https://www.colorsexplained.com/wp-content/uploads/2021/05/shades-of-green-color-infographic.jpg.webp",
"https://cf.shopee.tw/file/22b1fc845e5ce92481d08c79ffa29296",
"https://i2.momoshop.com.tw/1658857094/goodsimg/0009/593/640/9593640_O_m.webp",
"https://hk.rcmart.com/image/catalog/blog_images/sumiko_gurashi_rcmart_charator2.jpg",
"https://i3.momoshop.com.tw/1672305576/goodsimg/0008/998/717/8998717_O_m.webp",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmRiFJ6JP4P1rqOcKoIpW9p7UvK8oWmfRcew&usqp=CAU",
]
override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
myTableView.dataSource = self
}
}
// MARK: - UITableViewDelegate, UITableViewDataSource
extension TableViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageUrls.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? TableViewCell else { fatalError() }
cell.myLabel?.text = "\(indexPath.row)"
cell.myImageView.WW.downloadImage(with: imageUrls[indexPath.row])
return cell
}
}
import UIKit
final class TableViewCell: UITableViewCell {
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var myLabel: UILabel!
}