02-ContentView-6.swift 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import SwiftUI
  2. import Kingfisher
  3. struct ContentView: View {
  4. func url(at index: Int) -> URL? {
  5. let urlPrefix = "https://raw.githubusercontent.com/onevcat/Kingfisher-TestImages/master/DemoAppImage/Loading/kingfisher"
  6. return URL(string: "\(urlPrefix)-\(index + 1).jpg")
  7. }
  8. var body: some View {
  9. List {
  10. ForEach(0 ..< 10) { i in
  11. HStack {
  12. KFImage(url(at: i))
  13. .resizable()
  14. .roundCorner(
  15. radius: .widthFraction(0.5),
  16. roundingCorners: [.topLeft, .bottomRight]
  17. )
  18. .serialize(as: .PNG)
  19. .onSuccess { result in
  20. print("Image loaded from cache: \(result.cacheType)")
  21. }
  22. .onFailure { error in
  23. print("Error: \(error)")
  24. }
  25. .frame(width: 64, height: 64)
  26. Text("Index \(i)")
  27. }
  28. }
  29. }.listStyle(.plain)
  30. }
  31. }