在本教程的上一部分中,无涯教程构建了一个应用程序,需要在该应用程序中从URL下载图像(由API发送)并将其设置在图像视图上。但是,无涯教程已经使用tableview DataSource方法中的以下代码实时下载了所有图像。
cell.artistImgView.image = try UIImage(data: Data(contentsOf: URL(string: artistData.artworkUrl60!) ?? URL(string: "http://www.google.com")!))
如果无涯教程观察该应用程序,则在表视图中滚动浏览是一项非常繁琐的任务,因为图像是从主线程下载的。但是,如果无涯教程继续在运行时从图像URL下载和设置图像,该问题将继续存在。
要解决此问题,无涯教程可以使用AlamofireImage,它简化了图像下载任务。在这里,无涯教程可以使用它来下载图片,同时可以使用NSCache在下载后将映像添加到缓存中。
AlamofireImage在Alamofire的顶部运行,这意味着无涯教程需要安装这两个库才能与AlamofireImage一起使用。无涯教程可以使用Podfile安装AlamofireImage,在其中添加以下行。
pod ' AlamofireImage '
AlamofireImage的pod安装完成后,无涯教程可以使用import语句迅速导入它。
import Alamofire import AlamofireImage
要使用AlamofireImage下载图像,首先,无涯教程需要使用以下代码来设置缓存。
let imageCache = AutoPurgingImageCache( memoryCapacity: 111_111_111, preferredMemoryUsageAfterPurge: 90_000_000)
现在,无涯教程需要请求下载图像。为此,无涯教程使用Alamofire的request方法。
链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-image-caching-with-alamofireimage.html
来源:LearnFk无涯教程网
Alamofire.request(self.nameUrl[i]).responseImage { response in if response.result.value != nil { let image = UIImage(data: response.data!, scale: 1.0)! imageCache.add(image, withIdentifier: self.nameUrl[i]) } }
由于无涯教程已将图像添加到“Image Cache”中,因此需要在将其填充到图像视图时从缓存中检索回来。
if let image = imageCache.image(withIdentifier: self.nameUrl[self.a]){ self.localImageView.image = image }
在这里,无涯教程将继续在本教程的上一部分中创建的ArtistProject。但是,这次,无涯教程将要加载的所有图像存储在表视图中的图像缓存中,并在加载图像时使用该图像缓存。
ViewController.swift
import UIKit import Alamofire import AlamofireImage class ViewController: UIViewController { @IBOutlet weak var tableView: UITableView! var artist = Array() let imageCache = AutoPurgingImageCache(memoryCapacity: 111_111_111, preferredMemoryUsageAfterPurge: 90_000_000) override func viewDidLoad() { super.viewDidLoad() //Do any additional setup after loading the view. loadJsonData() tableView.delegate = self tableView.dataSource = self //tableView.rowHeight = UITableView.automaticDimension } func loadJsonData() { Alamofire.request("https://itunes.apple.com/search?media=music&term=bollywood").responseJSON { (response) in //print("Response value \(response)") do{ if(response.result.isSuccess){ let result: ArtistResponseModel = try JSONDecoder().decode(ArtistResponseModel.self, from: response.data!) debugPrint(result) self.artist = result.results ?? [] for i in result.results ?? []{ Alamofire.request(i.artworkUrl60!).responseImage { (response) in if response.result.value != nil{ let image = UIImage(data: response.data!, scale: 1.0) self.imageCache.add(image!, withIdentifier: i.artworkUrl60! ) } } } self.tableView.reloadData() } }catch{ } } } } extension ViewController : UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return artist.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell if(artist.count > 0){ let artistData = artist[indexPath.row] cell.artistImgView.image = imageCache.image(withIdentifier: artistData.artworkUrl60!) cell.trackName.text = artistData.trackName cell.artisName.text = artistData.artistName cell.artistCountry.text = artistData.country } return cell } } extension ViewController : UITableViewDelegate{ func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 220 } }
现在,如果无涯教程运行项目,则滚动时将不会遇到任何问题,因为这次无涯教程不在运行时下载图像。但是,无涯教程正在从缓存中检索图像。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)