KingFisher (SwiftUI 기본편)

yongbeom kwak·2022년 7월 26일

Kingfisher

목록 보기
1/1

KingFisher란?

1. 비동기 이미지 다운로드 및 캐싱

2. URLSession 기반 네트워킹

3. 메모리와 디스크를 위한 다중 계층 캐시 제공

4. GIF형식의 이미지 지원

5. 유용한 이미지 프로세서 & 필터 기능을 제공

6. Extension 형식으로 기능을 제공

7. SwiftUI도 제공

Installation

  1. PodFile
pod 'Kingfisher', '~> 7.0'
  1. Swift Packages
https://github.com/onevcat/Kingfisher.git

예제(SwiftUI)

  1. 기본 설정 가지고 놀기
import SwiftUI
import Kingfisher

struct ContentView: View {
    
    let url:URL = URL(string: "https://randomuser.me/api/portraits/men/50.jpg")!

     var body: some View {
          VStack{
              KFImage(url)
                  .placeholder { //플레이스 홀더 설정
                      Image(systemName: "list.dash")
                  }.retry(maxCount: 3, interval: .seconds(5)) //재시도
                  .onSuccess {r in //성공
                      print("succes: \(r)")
                  }
                  .onFailure { e in //실패
                      print("failure: \(e)")
                  }
                  .resizable()
                  .frame(width: 128, height: 128) //resize
                  .cornerRadius(20) //둥근 코너 설정
                  .shadow(radius: 5) // 그림자 설정


          }
      }
  }

}
  1. 낭비 방지

  List {
            ForEach(0..<20) { i in
                KFImage(URL(string: self.url + "\(i+1).jpg")!)
                    .cancelOnDisappear(true) //셀이 화면 밖에 있을 때는 다운로드 취소
            }
        }
profile
IOS개발 공부생

0개의 댓글