9
7

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 1 year has passed since last update.

【SwiftUI】AsyncImageを使ってURL画像を表示する

Posted at

はじめに

iOS15からAsyncImageが使用できます。
これを使用することでURL画像が表示できるようになります。
便利ですねー

ただiOS15からなので使えるようになるのはまだ先になりそうです。

実装方法

標準

import SwiftUI

struct ContentView: View {
    private let url = "https://s3-ap-northeast-1.amazonaws.com/qiita-image-store/0/1745371/16fffb96aaa9ea64e44d21f407f053ca3445a99b/x_large.png?1633600430"
    var body: some View {
        AsyncImage(url: URL(string: url))
    }
}

スクリーンショット 2022-11-18 13.42.56.png

サイズ変更(scale)

import SwiftUI

struct ContentView: View {
    private let url = "https://s3-ap-northeast-1.amazonaws.com/qiita-image-store/0/1745371/16fffb96aaa9ea64e44d21f407f053ca3445a99b/x_large.png?1633600430"
    var body: some View {
        AsyncImage(url: URL(string: url), scale: 5)
    }
}

スクリーンショット 2022-11-18 13.44.14.png

サイズ変更(frame)

import SwiftUI

struct ContentView: View {
    private let url = "https://s3-ap-northeast-1.amazonaws.com/qiita-image-store/0/1745371/16fffb96aaa9ea64e44d21f407f053ca3445a99b/x_large.png?1633600430"
    var body: some View {
        AsyncImage(url: URL(string: url)) { response in
            response.image?
                .resizable()
                .scaledToFit()
                .frame(width: 100, height: 100)
        }
    }
}

スクリーンショット 2022-11-18 13.46.33.png

プレイスホルダー

import SwiftUI

struct ContentView: View {
    private let url = "https://s3-ap-northeast-1.amazonaws.com/qiita-image-store/0/1745371/16fffb96aaa9ea64e44d21f407f053ca3445a99b/x_large.png?1633600430"
    var body: some View {
        AsyncImage(url: URL(string: url)) { image in
            image
                .resizable()
                .scaledToFit()
                .frame(width: 100, height: 100)
        } placeholder: {
            ProgressView()
        }
    }
}

12_AdobeExpress.gif

プレイスホルダー & 切り替えアニメーション

import SwiftUI

struct ContentView: View {
    private let url = "https://s3-ap-northeast-1.amazonaws.com/qiita-image-store/0/1745371/16fffb96aaa9ea64e44d21f407f053ca3445a99b/x_large.png?1633600430"
    var body: some View {
        AsyncImage(url: URL(string: url), transaction: .init(animation: .easeInOut(duration: 1))) { response in
            switch response {
            case .success(let image):
                image
                    .resizable()
                    .scaledToFit()
                    .frame(width: 100, height: 100)
            default:
                ProgressView()
            }
        }
    }
}

注意
Previewではアニメーションされません

画面収録_2022-11-18_14_19_17_AdobeExpress.gif

おわり

SwiftUIの完成はiOS20くらいかな??

9
7
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
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?