美文网首页
获取随机照片并保存

获取随机照片并保存

作者: 王勋才 | 来源:发表于2021-10-23 11:15 被阅读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)
                        
                })
                }
                  
                
               
    
            }
        }
    }
    
    
    
    

    相关文章

      网友评论

          本文标题:获取随机照片并保存

          本文链接:https://www.haomeiwen.com/subject/fttgoltx.html