1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SwiftUIでPhoto Libraryを使う

Posted at

Photo Libratyから写真を読み込む

iPhoneに保存されている写真を選択して表示するのを今回は、SwiftUIでやってみようと思います。

全体のコード:

import PhotosUI
import SwiftUI

struct PhotoLibrary: View {
    @State private var pickerItem: PhotosPickerItem?
    @State private var selectedImage: Image?
    
    var body: some View {
        VStack {
            PhotosPicker("Select a picture", selection: $pickerItem, matching: .images)
            
            selectedImage?
                .resizable()
                .scaledToFit()
        }
        .onChange(of: pickerItem) { Task {
            selectedImage = try await pickerItem?.loadTransferable(type: Image.self)
        }
        }
    }
}

#Preview {
    PhotoLibrary()
}

こんな感じになります。

最後に

今回は、端末に保存されている写真を選択して表示するのをやってみました。SwiftUIの学習でよくお世話になっているこちらの動画を参考に勉強しました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?