Kingfisher

hyun·2025년 7월 21일
0

iOS

목록 보기
31/54

 Kingfisher를 이용한 이미지 비동기 로드 & 캐싱

iOS 앱에서 Kingfisher를 활용해 TMDB 포스터 이미지 최적화 로드하기

 1. Kingfisher란?

Swift 기반 오픈소스 이미지 다운로드/캐싱 라이브러리
비동기 네트워크 로드, 메모리·디스크 자동 캐싱, 옵션 커스터마이징 지원
UIImageView, UIButton, MKAnnotationView 등 다양한 뷰에 바로 적용 가능

 2. 설치 방법

CocoaPods
pod 'Kingfisher', '~> 8.0'

or

// Swift Package Manager
"https://github.com/onevcat/Kingfisher.git"

 3. 기본 사용법

import

import Kingfisher

URL 준비

let url = URL(string: "https://image.tmdb.org/t/p/original\(posterPath)")

이미지 로드

UIImageView

imageView.kf.setImage(with: url)

UIButton 에도

button.kf.setImage(with: url, for: .normal)

 4. 프로젝트 적용

// MoviePosterCell.swift

func setImage(with path: String?) {
    guard let path = path,
          let url = URL(string: "https://image.tmdb.org/t/p/original\(path)")
    else { return }
    
    // 기본 로드 (비동기, placeholder·캐싱 자동)
    posterButton.kf.setImage(with: url, for: .normal)
}

posterButton 에 kf.setImage 호출

.normal 상태에 맞춰 이미지 세팅

Kingfisher가 백그라운드에서 다운로드 → 메모리/디스크에 캐싱

스크롤 시 재사용 셀에서도 즉시 캐시된 이미지를 보여줌

// 캐시 삭제
ImageCache.default.clearMemoryCache()
ImageCache.default.clearDiskCache()

0개의 댓글