SwiftUI 中使用SDWebImageSwiftUI加载网络图片

SDWebImageSwiftUI开源库GitHub地址

网络图片的加载

import SwiftUI

private let url = URL(string: "https://img2.baidu.com/it/u=1914391875,4121207940&fm=26&fmt=auto&gp=0.jpg")!
struct ContentView: View {
    var body: some View {
        NavigationView{
            List{
                NavigationLink(
                    destination: WebImageExample(url: url),
                    label: {
                        Text("WebImageExample")
                    })
            }
            .navigationBarHidden(true)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
import SwiftUI
import SDWebImageSwiftUI

struct WebImageExample: View {
    let url: URL?
    @State var isAnimating: Bool = true
    var body: some View {
        VStack {
            WebImage(url: url)
                .placeholder{Color.gray}
                .resizable()
                .onSuccess(perform: { _, _, _ in
                    print("Success")
                    SDWebImageManager.shared.imageCache.clear(with: .all, completion: nil)//清除图片缓存
                })
                .onFailure(perform: { _ in
                    print("Failure")
                })
                .scaledToFit()
                .frame(height:300)
                .clipped()
            
            WebImage(url: URL(string: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd24885b5b1a6643685ddba5bda3b17866b6c614c33fbd-XtBuP4_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1630205128&t=1964ae7a79ad3fcb3d4ca970b19b53b9"), isAnimating: $isAnimating) // 支持动图
                .customLoopCount(.max) // 播放次数
                .playbackRate(1.0) // 播放速度
                .playbackMode(.normal)
        }
    }
}
posted @ 2021-07-30 11:09  我不爱吃鱼  阅读(1621)  评论(0编辑  收藏  举报
Live2D