10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

iOSの画像ロードライブラリ「Nuke」のセットアップ&使い方(Swift)

Posted at

「Nuke」とは?

iOSやmacOSなど、Appleのプラットフォーム向けの画像ロードライブラリです。

画像ロードライブラリの比較

iOS向けの画像ロードライブラリを比較しました。

SDWebImage Kingfisher Nuke
最新バージョン 5.10.4 6.2.1 9.4.0
最終リリース 2021/02/02 2021/03/09 2021/03/27
スター数 23.5k 18.2k 5.8k
言語 Objective-C Swift Swift

※2021/03/27現在

この比較のみだとライブラリの選定基準にならないかもしれません。
以下の記事が参考になると思います。

私は特に深い理由はなく、Nukeを使ってみたかったため選定しました。

環境

  • OS:macOS Big Sur 11.1
  • Xcode:12.4 (12D4e)
  • Swift:5.3.2
  • Nuke:9.4.0

セットアップ

インストール

私はXcodeGen + SwiftPMを使っているため、 project.yml に以下を記述します。

project.yml
packages:
  Nuke:
    url: https://github.com/kean/Nuke
    version: 9.4.0

他の方法は 公式ページ をご参照ください。

使い方

ライブラリをインポートし、ロードするのみです。

以下のような拡張メソッドを用意すると便利です。

UIImageView+URL.swift
import UIKit
import Nuke

extension UIImageView {
    func loadImage(with urlString: String) {
        guard let url = URL(string: urlString) else {
            return
        }
        loadImage(with: url)
    }
    
    func loadImage(with url: URL) {
        Nuke.loadImage(with: url, into: self)
    }
}

おわりに

かんたんに画像をURLから取得して表示することができました!
詳細な使い方は公式ドキュメントをご参照ください。

参考リンク

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?