Using the KingFisher library in iOS development.
The Kingfisher library is a valuable tool for efficiently loading and displaying images in iOS applications. Since its emergence about 8 years ago, the iOS community has widely adopted it due to its simplicity, performance, and robust features. Kingfisher is an open-source library with over 200 contributors to date and more than 2,500 commits.
Advantages offered by the library:
Implementing the Kingfisher Library in iOS Development: A Practical Example
Recently, I integrated the library into my Pokedex project, which is simple but can experience delays in loading images due to the number of Pokémon to be displayed. The use of Kingfisher fits perfectly in this scenario. Here’s a practical implementation example:
领英推荐
import SwiftUI
import Kingfisher
struct PokemonCell: View {
let pokemon: PokemonModel
@State private var image: UIImage?
var body: some View {
let color = Color.pokemon(type: pokemon.pokemonType)
ZStack {
VStack {
HStack {
title
Spacer()
type
}
.padding(.top, 10)
.padding(.horizontal, 10)
if let url = URL(string: pokemon.imageUrl) {
KFImage(url)
.placeholder {
ProgressView()
}
.resizable()
.scaledToFit()
.frame(width: 130, height: 150)
.padding(10)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(color)
.cornerRadius(12)
.shadow(color: color.opacity(0.7), radius: 6, x: 0.0, y: 0.0)
}
var title: some View {
Text(pokemon.name.capitalized)
.font(.headline).bold()
.foregroundColor(.white)
}
var type: some View {
Text(pokemon.pokemonType.rawValue)
.font(.subheadline).bold()
.foregroundColor(.white)
.padding(.horizontal, 10)
.padding(.vertical, 6)
.overlay(
RoundedRectangle(cornerRadius: 20)
.fill(Color.white.opacity(0.25))
)
}
}
#Preview {
HomeView(viewModel: homeViewModel())
}
As you can see, the implementation is straightforward. Follow the steps in this example:
And with that, you already have everything you need!