0
0

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のPhotosPickerの画像が逆さまになってしまった時

Posted at

問題

SwiftUIのPhotosPickerを利用した場合謎に画像が逆さまになってしまっている

@State private var transactionImageItem: PhotosPickerItem?
@State private var transactionImage: Image?

var body: some View {
    NavigationStack {
        VStack {
            HStack {
                VStack {
                    if transactionImage == nil {
                        PhotosPicker("Select avatar", selection: $transactionImageItem, matching: .images)
                    } else {
                       transactionImage?
                           .resizable()
                            .scaledToFit()
                            .frame(width: 100, height: 100)
                    }
                }
                .padding(.horizontal, 10)
            }
        }
        .onChange(of: transactionImageItem) {
                Task {
                    if let image = try? await transactionImageItem?.loadTransferable(type: Image.self) {
                        transactionImage = image
                    } else {
                        print("Failed")
                    }
                }
            }
    }
}

こうするとこんな感じになってしまう

解決

一旦Data型で取得して、Data -> UIImage -> Imageにしたら直りました。なあぜなあぜ :thinking:

let data = try? await transactionImageItem?.loadTransferable(type: Data.self),
let image = UIImage(data: data) {
transactionImage = Image(uiImage: image)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?