获取随机照片并保存

2021-10-23  本文已影响0人  王勋才
//
//  PhotorandomView.swift
//  TestAppForIos
//
//  Created by wangxuncai on 2021/10/17.
//

import SwiftUI

struct PhotorandomView: View {
    @State var image:UIImage? = nil
    var body: some View {
        ZStack{
           
            Image(uiImage: image ?? UIImage(named: "666")!)
                .resizable()
                .scaledToFill()
                .ignoresSafeArea(edges: .all)
            
            VStack {
                Spacer()
                Button(action: {
                    let url = URL(string: "https://source.unsplash.com/random")!
                    guard let data = try? Data(contentsOf: url) else{return}
                    image = UIImage(data: data)
                }, label: {
                    Text("换壁纸")
                        .font(.title)
                        .foregroundColor(.white)
                        .frame(height:55)
                        .frame(width:200)
                        .background(Material.thinMaterial)//背景色上色放最后
                        .cornerRadius(10)
                        .padding(.horizontal,10)
                    
            })
                Button(action: {
                    if let im = image {
                        UIImageWriteToSavedPhotosAlbum(im, nil, nil, nil)
                    }
                 
                }, label: {
                    Text("保存")
                        .font(.title)
                        .foregroundColor(.white)
                        .frame(height:55)
                        .frame(width:200)
                        .background(Material.thinMaterial)//背景色上色放最后
                        .cornerRadius(10)
                        .padding(.horizontal,10)
                    
            })
            }
              
            
           

        }
    }
}



上一篇下一篇

猜你喜欢

热点阅读