GettingStartedSwiftUI.tutorial 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. @Tutorial(time: 10) {
  2. @Intro(title: "Getting Started with Kingfisher (SwiftUI)") {
  3. Installs Kingfisher and 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-4.png, alt:"The first image is loaded into the image view in cell.")
  16. }
  17. }
  18. @Section(title: "Installing") {
  19. @ContentAndMedia {
  20. After creating your SwiftUI app, the first step is to install Kingfisher. For this, we use Swift Package Manager.
  21. > There are also other way to add Kingfisher to your project, such as CocoaPods or manually. Check the related documentation for more information.
  22. @Image(source: create-project-swiftui.png, alt: "")
  23. }
  24. @Steps {
  25. @Step {
  26. Choose "File" → "Add Package Dependencies…". In the pop-up window, enter the URL below to the search
  27. bar, and click the "Add Package" button.
  28. `https://github.com/onevcat/Kingfisher.git`
  29. @Image(source: add-dependency.png, alt: "Add Kingfisher as the dependency of your project.")
  30. }
  31. @Step {
  32. After downloading, add the `Kingfisher` library to your created project.
  33. @Image(source: add-to-project.png, alt: "")
  34. }
  35. @Step {
  36. 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.
  37. @Image(source: add-library-swiftui.png, alt: "")
  38. }
  39. @Step {
  40. To verify the installation. Choose "ContentView.swift" file.
  41. @Code(name: "ContentView.swift", file: 02-ContentView-1.swift)
  42. }
  43. @Step {
  44. Import `Kingfisher`. And try to print the `KingfisherManager.shared` in the `onAppear`. If you see
  45. something like "Kingfisher.KingfisherManager" in the Xcode debugger console, it means Kingfisher is
  46. ready in your project.
  47. @Code(name: "ContentView.swift", file: 02-ContentView-2.swift)
  48. }
  49. }
  50. }
  51. @Section(title: "Loading image with Kingfisher") {
  52. @ContentAndMedia {
  53. In this section, we will create a `List` and use Kingfisher to load some images from the network.
  54. }
  55. @Steps {
  56. @Step {
  57. Setting up a `List` in SwiftUI is easy. With the `ContentView` from the SwiftUI template.
  58. @Code(name: "ContentView.swift", file: 02-ContentView-2.swift)
  59. }
  60. @Step {
  61. Replace the `body` of the `ContentView` with a `List` and the embedded `ForEach`.
  62. @Code(name: "ContentView.swift", file: 02-ContentView-3.swift) {
  63. @Image(source: preview-1-swiftui.png, alt: "")
  64. }
  65. }
  66. @Step {
  67. To load an image from network, the easiest way is using the `KFImage` struct provided in Kingfisher. It
  68. accepts a URL, loads and shows the image when its `onAppear` is called. `KFImage` has a set of similar
  69. APIs to SwiftUI's `Image` type. Here we call `resizable()` to allow the image fit into the given frame.
  70. @Code(name: "ContentView.swift", file: 02-ContentView-4.swift) {
  71. @Image(source: preview-2-swiftui.png, alt: "")
  72. }
  73. }
  74. }
  75. }
  76. @Section(title: "Loading Options") {
  77. }
  78. }