GettingStartedUIKit.tutorial 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. @Tutorial(time: 15) {
  2. @Intro(title: "Getting Started with Kingfisher (UIKit)") {
  3. Install Kingfisher and learn basic usage of the framework with UIKit.
  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 UITableView list that displays rounded images of kingfisher
  9. birds, downloaded using the Kingfisher library. It includes:
  10. - Setting Up `UITableView`: 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-4.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 UIKit 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.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.png, alt: "")
  39. }
  40. @Step {
  41. To verify the installation. Choose "ViewController.swift" file.
  42. @Code(name: "ViewController.swift", file: 01-ViewController-1.swift)
  43. }
  44. @Step {
  45. Import `Kingfisher`. And try to print the `KingfisherManager.shared`. If you see something like
  46. "Kingfisher.KingfisherManager" in the Xcode debugger console, it means Kingfisher is ready in your
  47. project.
  48. @Code(name: "ViewController.swift", file: 01-ViewController-2.swift)
  49. }
  50. }
  51. }
  52. @Section(title: "Creating the Table View") {
  53. @ContentAndMedia {
  54. Creating and setting up `UITableView` is not the focus of this tutorial, as it does not involve Kingfisher.
  55. However, we will later use Kingfisher to manage images in the `UIImageView` within the table cells.
  56. @Image(source: preview-1.png, alt: "")
  57. }
  58. @Steps {
  59. @Step {
  60. Create a `SampleCell` file. We will use it as the cell type of the table view.
  61. @Code(name: "SampleCell.swift", file: 01-SampleCell-1.swift)
  62. }
  63. @Step {
  64. Add a `sampleImageView` to the class. It is the main target image view we are going to set later.
  65. @Code(name: "SampleCell.swift", file: 01-SampleCell-2.swift)
  66. }
  67. @Step {
  68. Add other necessary views and layout code. (Boring, just copy it!)
  69. @Code(name: "SampleCell.swift", file: 01-SampleCell-3.swift)
  70. }
  71. @Step {
  72. In the "ViewController.swift".
  73. @Code(name: "ViewController.swift", file: 01-ViewController-3.swift)
  74. }
  75. @Step {
  76. Add a `tableView` to the view controller.
  77. @Code(name: "ViewController.swift", file: 01-ViewController-4.swift)
  78. }
  79. @Step {
  80. Extend `ViewController` to conform the `UITableViewDataSource`. For the sake of simplicity, we will
  81. only return one cell at first.
  82. @Code(name: "ViewController.swift", file: 01-ViewController-5.swift) {
  83. @Image(source:preview-1.png, alt:"An iOS app with a list which contains a single cell.")
  84. }
  85. Run the app, now you should see a list which contains a single cell with a light grey placeholder and a
  86. text label.
  87. }
  88. }
  89. }
  90. @Section(title: "Loading image with Kingfisher") {
  91. @ContentAndMedia {
  92. Kingfisher simplifies the task of loading images from remote URLs. It also offers a range of user-friendly
  93. processors and helper methods. In this section, we will cover how to use these features to streamline common
  94. tasks.
  95. @Image(source: preview-4.png, alt: "")
  96. }
  97. @Steps {
  98. @Step {
  99. The simplest way to start loading a remote image into an image view in Kingfisher, is using the `kf`
  100. wrapper and its method. In the code above, we already have a `sampleImageView` in the cell.
  101. @Code(name: "ViewController.swift", file: 01-ViewController-6-0.swift)
  102. }
  103. @Step {
  104. To load the first image, call `kf.setImage(with:)` on the image view, with the desired URL.
  105. @Code(name: "ViewController.swift", file: 01-ViewController-6.swift) {
  106. @Image(source:preview-2.png, alt:"The first image is loaded into the image view in cell.")
  107. }
  108. Now, running the app again, you can see the image is already loaded and set to the image view.
  109. }
  110. @Step {
  111. To actually load the images based on the index, we can try to add more cells. We prepared 10 kingfisher
  112. images, let's change the item count to 10:
  113. @Code(name: "ViewController.swift", file: 01-ViewController-7.swift) {
  114. @Image(source:preview-3.png, alt:"The first image is loaded into the image view in cell.")
  115. }
  116. Kingfisher also downloads and caches these images. Now, even if you turn off the network of
  117. your iOS device (or the simulator), and restart the app, these images can be loaded from cache and
  118. still displayed.
  119. }
  120. @Step {
  121. Kingfisher also comes with a bundle of useful processors and helper methods. For example, we can add a
  122. loading indicator and some partial round corner effect easily.
  123. @Code(name: "ViewController.swift", file: 01-ViewController-8.swift) {
  124. @Image(source:preview-4.png, alt:"The first image is loaded into the image view in cell.")
  125. }
  126. Besides of the `RoundCornerImageProcessor`, we also apply a `pngSerializer` to forcibly convert the
  127. loaded JPG file to PNG format when storing in the disk cache. This is necessary since JPG format does
  128. not contain an alpha channel, which is necessary when storing a round corner image.
  129. }
  130. @Step {
  131. The `setImage(with:)` method accepts other parameters, including a completion handler too. Let us add
  132. some logs before we continue to the next section.
  133. @Code(name: "ViewController.swift", file: 01-ViewController-9.swift)
  134. Now, running the app again, in the console you can see some text like "Image loaded from cache: disk".
  135. This is because the images are already in the disk cache, and they are loaded from the disk locally. By
  136. scrolling the table view up and down and triggering the cell reuse, it should print things like "Image
  137. loaded from cache: memory", which indicates the images are already cache in memory.
  138. }
  139. }
  140. }
  141. @Section(title: "Manipulating the Cache") {
  142. @ContentAndMedia {
  143. In this final part, we'll look at basic tasks related to image caching, like finding out the size of the
  144. disk cache and clearing all the cache. Usually, Kingfisher handles cache management automatically, so you
  145. don't need to think about it much. But if you need more detailed control over how caching works, this
  146. section will give you helpful tips and information.
  147. }
  148. @Steps {
  149. @Step {
  150. First, we need to find out how much space the image cache is using. Normally, you would check the cache
  151. size or clear the cache using a button. But to keep things simple, we won't add any extra buttons to
  152. this example.
  153. @Code(name: "ViewController.swift", file: 01-ViewController-10.swift)
  154. }
  155. @Step {
  156. In the `viewDidLoad` method, we use the `asyncAfter` method on the `DispatchQueue.main` queue. There we
  157. start a process to calculate the current size of the disk cache. The size we get tells us how much disk
  158. space Kingfisher is using for caching images.
  159. @Code(name: "ViewController.swift", file: 01-ViewController-11.swift)
  160. }
  161. @Step {
  162. To make it clear, we can create an alert and display it to the user, with a button to clear the cache
  163. manually.
  164. @Code(name: "ViewController.swift", file: 01-ViewController-12.swift) {
  165. @Image(source:preview-5.png, alt:"An alert which shows the disk cache size used by Kingfisher, with a button to purge the cache.")
  166. }
  167. }
  168. @Step {
  169. Lastly, use the `clearCache` function to remove all images from both the memory and disk caches. After
  170. this, when you refresh the table view's data, the images will be downloaded again from the internet.
  171. You'll notice "Image loaded from cache: none" displayed in the console, indicating the images are not
  172. being loaded from the cache this time.
  173. @Code(name: "ViewController.swift", file: 01-ViewController-13.swift)
  174. The cache cleaning is only for demonstration purpose. In practice, usually you do not need to call it yourself. Kingfisher will manage and purge the data based on its default policy.
  175. }
  176. }
  177. }
  178. @Section(title: "Next Steps") {
  179. @ContentAndMedia {
  180. Congratulations!
  181. You have now mastered some basic uses of Kingfisher: including loading images from the web or cache using
  182. the `UIImageView` extension, processing images before display using ``ImageProcessor``, and basic methods
  183. for inspecting and clearing the cache.
  184. Kingfisher also contains a considerable number of other features, and it has been designed to be simple to
  185. use while considering flexibility. As you deepen your understanding of the framework, we hope you will
  186. gradually grow to like it.
  187. Next, we recommend that you start using Kingfisher in your projects to help you accomplish tasks. You can
  188. also read the <doc:CommonTasks> and its related articles to get a better understanding. When
  189. you encounter problems, come back to consult the documentation or ask the community.
  190. Have a nice day!
  191. }
  192. @Steps { }
  193. }
  194. }