ContentView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) DreamPiggy <lizhuoli1126@126.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. import SwiftUI
  9. import RealityKit
  10. import RealityKitContent
  11. import SDWebImage
  12. import SDWebImageSwiftUI
  13. struct ContentView: View {
  14. @State var imageURLs = [
  15. "http://assets.sbnation.com/assets/2512203/dogflops.gif",
  16. "https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
  17. "http://apng.onevcat.com/assets/elephant.png",
  18. "http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",
  19. "http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",
  20. "http://littlesvr.ca/apng/images/SteamEngine.webp",
  21. "http://littlesvr.ca/apng/images/world-cup-2014-42.webp",
  22. "https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp",
  23. "https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic",
  24. "https://nokiatech.github.io/heif/content/image_sequences/starfield_animation.heic",
  25. "https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
  26. "https://raw.githubusercontent.com/ibireme/YYImage/master/Demo/YYImageDemo/mew_baseline.jpg",
  27. "https://via.placeholder.com/200x200.jpg",
  28. "https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_5.jpg",
  29. "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/w3c.svg",
  30. "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/wikimedia.svg",
  31. "https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/stack_of_photos.pdf",
  32. "https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/smartphone_tablet.pdf"
  33. ]
  34. @State var animated: Bool = false // You can change between WebImage/AnimatedImage
  35. // Used to avoid https://twitter.com/fatbobman/status/1572507700436807683?s=20&t=5rfj6BUza5Jii-ynQatCFA
  36. struct ItemView: View {
  37. @Binding var animated: Bool
  38. @State var url: String
  39. var body: some View {
  40. NavigationLink(destination: DetailView(url: url, animated: self.animated)) {
  41. HStack {
  42. if self.animated {
  43. AnimatedImage(url: URL(string:url))
  44. .indicator(.activity)
  45. .transition(.fade)
  46. .resizable()
  47. .scaledToFit()
  48. .frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
  49. } else {
  50. WebImage(url: URL(string:url))
  51. .resizable()
  52. .indicator(.activity)
  53. .transition(.fade(duration: 0.5))
  54. .scaledToFit()
  55. .frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
  56. }
  57. Text((url as NSString).lastPathComponent)
  58. }
  59. }
  60. .buttonStyle(PlainButtonStyle())
  61. }
  62. }
  63. var body: some View {
  64. return NavigationView {
  65. contentView()
  66. .navigationBarTitle(animated ? "AnimatedImage" : "WebImage")
  67. .navigationBarItems(leading:
  68. Button(action: { self.reloadCache() }) {
  69. Text("Reload")
  70. }, trailing:
  71. Button(action: { self.switchView() }) {
  72. Text("Switch")
  73. }
  74. )
  75. }
  76. }
  77. func contentView() -> some View {
  78. List {
  79. ForEach(imageURLs, id: \.self) { url in
  80. // Must use top level view instead of inlined view structure
  81. ItemView(animated: $animated, url: url)
  82. }
  83. .onDelete { indexSet in
  84. indexSet.forEach { index in
  85. self.imageURLs.remove(at: index)
  86. }
  87. }
  88. }
  89. }
  90. func reloadCache() {
  91. SDImageCache.shared.clearMemory()
  92. SDImageCache.shared.clearDisk(onCompletion: nil)
  93. }
  94. func switchView() {
  95. SDImageCache.shared.clearMemory()
  96. animated.toggle()
  97. }
  98. }
  99. #if DEBUG
  100. struct ContentView_Previews: PreviewProvider {
  101. static var previews: some View {
  102. ContentView()
  103. }
  104. }
  105. #endif