简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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