简体   繁体   English

如何将图像保存到 ImageView 中显示的照片库(SwiftUI)

[英]How to save an image to photo library that is displayed in ImageView (SwiftUI)

What should I write to capture the same image that is displayed then save it to photo library我应该写什么来捕获显示的相同图像然后将其保存到照片库

Code View代码视图

This is an image displayed in view.这是显示在视图中的图像。 I want to save the displayed image in photo library.我想将显示的图像保存在照片库中。

Simulator View模拟器视图

You don't call the "save" function anywhere.您不会在任何地方调用“保存”function。 You can call it, when you tapped on this image当你点击这张图片时,你可以调用它

struct SwiftUIView: View {
    var body: some View {
        HStack {
            Image("car1")
                .onTapGesture {
                    save()
                }
        }
    }
    
    func save(){
        UIImageWriteToSavedPhotosAlbum(UIImage(named: "car1")!, nil, nil, nil)
    }
}

Or create the button in HStack or VStack或者在 HStack 或 VStack 中创建按钮

struct SwiftUIView: View {
    var body: some View {
        HStack {
            Image("car1")
            Button("Save") {
                save()
            }
        }
    }
    
    func save(){
        UIImageWriteToSavedPhotosAlbum(UIImage(named: "car1")!, nil, nil, nil)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM