GettingStartedSwiftUI.tutorial 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. @Tutorial(time: 10) {
  2. @Intro(title: "Getting Started with Kingfisher (SwiftUI)") {
  3. Install Kingfisher and learn basic usage of the framework with SwiftUI.
  4. @Image(source: getting-started-card, alt: "Title image of the tutorial. A kingfisher bird standing on a tree.")
  5. }
  6. @Section(title: "Overview") {
  7. @ContentAndMedia {
  8. This tutorial guides you through building a SwiftUI `List` that displays rounded images of kingfisher birds,
  9. downloaded using the Kingfisher library. It includes:
  10. - Setting Up `List`: Quick setup for a basic list.
  11. - Using Kingfisher: Download and display bird images.
  12. - Image Processing: Convert images to rounded corners for display.
  13. - Cache Size Button: A feature to check cache usage.
  14. At the final stage of this tutorial, you will have a list like this:
  15. @Image(source:preview-3-swiftui.png, alt:"The first image is loaded into the image view in cell.")
  16. }
  17. @Steps { }
  18. }
  19. @Section(title: "Installing") {
  20. @ContentAndMedia {
  21. After creating your SwiftUI app, the first step is to install Kingfisher. For this, we use Swift Package Manager.
  22. > There are also other way to add Kingfisher to your project, such as CocoaPods or manually. Check the related documentation for more information.
  23. @Image(source: create-project-swiftui.png, alt: "")
  24. }
  25. @Steps {
  26. @Step {
  27. Choose "File" → "Add Package Dependencies…". In the pop-up window, enter the URL below to the search
  28. bar, and click the "Add Package" button.
  29. `https://github.com/onevcat/Kingfisher.git`
  30. @Image(source: add-dependency.png, alt: "Add Kingfisher as the dependency of your project.")
  31. }
  32. @Step {
  33. After downloading, add the `Kingfisher` library to your created project.
  34. @Image(source: add-to-project.png, alt: "")
  35. }
  36. @Step {
  37. Select your app target in the "project and target list", switch to the "Build Phases" tab, expand the "Link Binary With Libraries" section, and confirm that "Kingfisher" is added. If not, click the "+" button and add it to the list.
  38. @Image(source: add-library-swiftui.png, alt: "")
  39. }
  40. @Step {
  41. To verify the installation. Choose "ContentView.swift" file.
  42. @Code(name: "ContentView.swift", file: 02-ContentView-1.swift)
  43. }
  44. @Step {
  45. Import `Kingfisher`. And try to print the `KingfisherManager.shared` in the `onAppear`. If you see
  46. something like "Kingfisher.KingfisherManager" in the Xcode debugger console, it means Kingfisher is
  47. ready in your project.
  48. @Code(name: "ContentView.swift", file: 02-ContentView-2.swift)
  49. }
  50. }
  51. }
  52. @Section(title: "Loading image with Kingfisher") {
  53. @ContentAndMedia {
  54. In this section, we will create a `List` and use Kingfisher to load some images from the network.
  55. }
  56. @Steps {
  57. @Step {
  58. Setting up a `List` in SwiftUI is easy. With the `ContentView` from the SwiftUI template.
  59. @Code(name: "ContentView.swift", file: 02-ContentView-2.swift)
  60. }
  61. @Step {
  62. Replace the `body` of the `ContentView` with a `List` and the embedded `ForEach`.
  63. @Code(name: "ContentView.swift", file: 02-ContentView-3.swift) {
  64. @Image(source: preview-1-swiftui.png, alt: "")
  65. }
  66. }
  67. @Step {
  68. To load an image from network, the easiest way is using the `KFImage` struct provided in Kingfisher. It
  69. accepts a URL, loads and shows the image when its `onAppear` is called. `KFImage` has a set of similar
  70. APIs to SwiftUI's `Image` type. Here we call `resizable()` to allow the image fit into the given frame.
  71. @Code(name: "ContentView.swift", file: 02-ContentView-4.swift) {
  72. @Image(source: preview-2-swiftui.png, alt: "")
  73. }
  74. }
  75. @Step {
  76. Kingfisher also comes with a bundle of useful processors and helper methods. For example, we can add
  77. some partial round corner effect in a simple way.
  78. @Code(name: "ContentView.swift", file: 02-ContentView-5.swift) {
  79. @Image(source:preview-3-swiftui.png, alt:"")
  80. }
  81. Besides of the `.roundCorner`, we also apply a `.serialize(as: .PNG)` to forcibly convert the
  82. loaded JPG file to PNG format when storing in the disk cache. This is necessary since JPG format does
  83. not contain an alpha channel, which is necessary when storing a round corner image.
  84. }
  85. @Step {
  86. The `KFImage` has a few other modifiers too, including some life cycle handlers like
  87. `.onSuccess` or `.onFailure`.
  88. @Code(name: "ContentView.swift", file: 02-ContentView-6.swift)
  89. Restart the app. If the images were loaded during your previous use of the app, you should see
  90. "Image loaded from cache: disk" in the Xcode console.
  91. }
  92. }
  93. }
  94. @Section(title: "Manipulating the Cache") {
  95. @ContentAndMedia {
  96. In this final part, we'll look at basic tasks related to image caching, like finding out the size of the
  97. disk cache and clearing all the cache. Usually, Kingfisher handles cache management automatically, so you
  98. don't need to think about it much. But if you need more detailed control over how caching works, this
  99. section will give you helpful tips and information.
  100. }
  101. @Steps {
  102. @Step {
  103. First, we need to find out how much space the image cache is using. Normally, you would check the cache
  104. size or clear the cache using a button. In this example, we will add a button to the first row of the
  105. list.
  106. @Code(name: "ContentView.swift", file: 02-ContentView-7.swift)
  107. }
  108. @Step {
  109. Add a `Button` at the top of the `List`. In its event block, call `calculateDiskStorageSize(completion:)`
  110. and print the disk cache size.
  111. @Code(name: "ContentView.swift", file: 02-ContentView-8.swift) {
  112. @Image(source:preview-4-swiftui.png, alt:"Added a button to the top of the list.")
  113. }
  114. }
  115. @Step {
  116. To trigger an alert in SwiftUI, we add two `@State` to the `ContentView`. In the `calculateDiskStorageSize`
  117. handler, we set both states.
  118. @Code(name: "ContentView.swift", file: 02-ContentView-9.swift)
  119. }
  120. @Step {
  121. Add an `.alert` modifier to the `Button`. When the `showAlert` state is set, an alert with the information
  122. of disk cache size is presented.
  123. @Code(name: "ContentView.swift", file: 02-ContentView-10.swift)
  124. }
  125. @Step {
  126. Lastly, use the `clearCache` function to remove all images from both the memory and disk caches. After
  127. this, when you restart the app or trigger a full reloading for the `List`, the images will be downloaded
  128. again from the internet. You'll notice "Image loaded from cache: none" displayed in the console,
  129. indicating the images are not being loaded from the cache this time.
  130. @Code(name: "ContentView.swift", file: 02-ContentView-11.swift) {
  131. @Image(source:preview-5-swiftui.png, alt:"An alert which shows the disk cache size used by Kingfisher, with a button to purge the cache.")
  132. }
  133. The cache cleaning is only for demonstration purpose. In practice, usually you do not need to call it
  134. yourself. Kingfisher will manage and purge the data based on its default policy.
  135. }
  136. }
  137. }
  138. @Section(title: "Next Steps") {
  139. @ContentAndMedia {
  140. Congratulations!
  141. You have now mastered some basic uses of Kingfisher: including loading images from the web or cache using
  142. the `KFImage` type, processing images before display using a modifier, and basic methods
  143. for inspecting and clearing the cache.
  144. Kingfisher also contains a considerable number of other features, and it has been designed to be simple to
  145. use while considering flexibility. As you deepen your understanding of the framework, we hope you will
  146. gradually grow to like it.
  147. Next, we recommend that you start using Kingfisher in your projects to help you accomplish tasks. You can
  148. also read the <doc:CommonTasks> and its related articles to get a better understanding. When
  149. you encounter problems, come back to consult the documentation or ask the community.
  150. Have a nice day!
  151. }
  152. @Steps { }
  153. }
  154. }