Как использовать подогреватель Nuke для кэширования изображений в ячейке таблицы: iOS - PullRequest
0 голосов
/ 15 мая 2019

My UITabelView медленная загрузка и прокрутка также медленная. Я получаю изображения из Url с ядерным оружием в UITableViewCell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = carTableView.dequeueReusableCell(withIdentifier: "CarDetailsTableViewCell") as! CarDetailsTableViewCell
    if let model = Obj![indexPath.row].model{
         cell.nameLabel.text = model
    }

    if let url = Obj![indexPath.row].image{
         let imageURL = URL(string: url)
         Nuke.loadImage(with: imageURL!, into: cell.carImage)

    }
    return cell
}

Ответы [ 2 ]

0 голосов
/ 15 мая 2019

Чтобы предотвратить медленную загрузку / прокрутку, используйте изображение-заполнитель. Пример кода ниже показывает, как разогреть изображения.

class YourViewController: UIViewController {
  var preheater = ImagePreheater(pipeline: ImagePipeline.shared)
  var requests: [ImageRequest]?
  // You need to populate remoteImages with the URLs you want prefetched
  var remoteImages = [URL]()

  override func viewDidLoad() {
    super.viewDidLoad()
    fetchImages()
  }

  func fetchImages() {
    requests = remoteImages.map {
      var request = ImageRequest(url: $0)
      request.priority = .high
      return request
    }
    if let requests = requests, requests.count > 0 {
      preheater.startPreheating(with: requests)
    }
  }

  deinit {
    if let requests = requests, requests.count > 0 {
      preheater.stopPreheating(with: requests)
    }
  }

  // This is your code modified to use placeholder

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = carTableView.dequeueReusableCell(withIdentifier: "CarDetailsTableViewCell") as! CarDetailsTableViewCell
    if let model = Obj![indexPath.row].model{
     cell.nameLabel.text = model
    }

    if let url = Obj![indexPath.row].image{
      let imageURL = URL(string: url)
      Nuke.loadImage(with: imageURL!, options: ImageLoadingOptions(
        placeholder: UIImage(named: "your-placeholder-image"),
        transition: .fadeIn(duration: 0.20)
      ), into: cell.carImage)
    }

    return cell
  }

}
0 голосов
/ 15 мая 2019

Загрузка изображений с помощью Nuke очень проста. Вы можете следовать приведенному ниже фрагменту кода:

    import Nuke

    Nuke.loadImage(with: URL(string: "http://yourImageUrl.jpeg"), into: cell.profileImage)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...