10-5 Kingfisher

STONE·2024년 12월 27일

Swift_Ios

목록 보기
35/44

주제

Kingfisher

이번 과제하면서 네트워크에서 이미지를 불러와서 앱에서 표시하는 작업을 할때 Kingfisher를 사용하게 되어 알아보려고 한다. Kingfisher는 Swift로 작성된 강력하고 사용하기 쉬운 라이브러리로 이미지 다운로드와 캐싱을 간단하게 처리할 수 있도록 도와준다

Kingfisher의 주요 기능

  1. 이미지 다운로드
imageView.kf.setImage(with: URL(string: "https://example.com/image.png"))
  1. 이미지 캐싱
    이미 다운로드된 이미지를 디스크와 메모리에 캐싱하여 불필요한 네트워크 요청을 방지합니다
    앱의 성능을 크게 향상시킬 수 있습니다
  2. 플레이스홀더 이미지
    이미지가 로드되는 동안 표시할 기본 이미지를 설정할 수 있다.
imageView.kf.setImage(
    with: URL(string: "https://example.com/image.png"),
    placeholder: UIImage(named: "placeholder")
)
  1. 비동기 처리
    이미지를 백그라운드에서 다운로드하고 메인 스레드에서 표시하여 UI가 블로킹되지 않도록 보장합니다.
  2. 다운로드 취소
    네트워크 요청을 쉽게 취소할 수 있습니다.
  3. 이미지 프로세싱
    다운로드된 이미지를 변형하거나 필터를 적용할 수 있습니다. 예를 들어, 크기 조정, 원형 처리, 블러 효과 등을 적용할 수 있습니다.

사용예시

// 기본 이미지 로드
import Kingfisher

let url = URL(string: "https://example.com/image.png")
imageView.kf.setImage(with: url)
//플레이스홀더 이미지와 오류 처리
let url = URL(string: "https://example.com/image.png")
imageView.kf.setImage(
    with: url,
    placeholder: UIImage(named: "placeholder"),
    options: nil,
    completionHandler: { result in
        switch result {
        case .success(let value):
            print("Image loaded successfully: \(value.image)")
        case .failure(let error):
            print("Error loading image: \(error.localizedDescription)")
        }
    }
)
//이미지 변환 적용
imageView.kf.setImage(
    with: URL(string: "https://example.com/image.png"),
    options: [.processor(RoundCornerImageProcessor(cornerRadius: 20))]
)

설치

  1. SPM
https://github.com/onevcat/Kingfisher.git
  1. CocoaPods
pod 'Kingfisher'
pod install

장점

  1. 성능 최적화: 이미지 캐싱을 통해 네트워크 사용량 감소
  2. 사용 편의성: 간단한 API 설계로 빠르게 구현 가능
  3. 확장성: 다양한 옵션과 커스터마이징 가능
profile
흠...?

0개의 댓글