Skip to content

Instantly share code, notes, and snippets.

@linearhw
Last active May 8, 2018 09:18
Show Gist options
  • Save linearhw/aa7feb7dd4f15d8dba2b6f6be104ccf9 to your computer and use it in GitHub Desktop.
Save linearhw/aa7feb7dd4f15d8dba2b6f6be104ccf9 to your computer and use it in GitHub Desktop.
AlamofireImage 기능 요약/정리

AlamofireImage

캐시 저장

  • af_setImage method 를 사용하면, URLCache (애플 기본 클래스) 와 AutoPurgingImageCache (Dictionary) 를 사용한다.
  • URLCache 는 원본을 갖고 있고, AutoPurgingImageCache 는 filter 를 씌운 결과를 가지고 있다.
  • AutoPurgingImageCache 는 cachedImages 라는 array 를 들고 있다. disk 는 사용하지 않는다.
  • 필터 기능을 사용하지 않는다면 URLSessionConfiguration 에서 URLCache 를 nil로 설정해야 불필요한 두 번 저장을 피할 수 있다.

옵션

  • URLCache : 20MB 메모리와 150MB 디스크 공간을 사용할 수 있다. URLSessionConfiguration 에서 조정할 수 있음.
  • AutoPurgingImageCache : 100MB 메모리 공간을 사용할 수 있다. cache init 할 때 조정할 수 있음.

메모리 관리

  • memory warning notification 을 받으면 AutoPurgingImageCache 의 removeAllImages 를 호출한다.
  • AutoPurgingImageCache 에 이미지를 add 했을 때, 메모리 용량을 설정값보다 많이 차지하고 있는 경우 lastAccessDate 로 정렬하여 지운다.
  • URLCache 는 지워주는 부분이 따로 없기 때문에 직접 지워줘야 한다.
  • expire 정책도 없다.

이미지 프로세싱

썸네일 처리

  • 별도로 없음 (필요하다면 AutoPurgingImageCache 를 직접 만들어서 쓰도록 안내하고 있음)

그 외의 전처리

  • Rounded Corner, Scale, Blur 등을 제공하고 있음
let url = URL(string: "https://httpbin.org/image/png")!
let placeholderImage = UIImage(named: "placeholder")!

let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
    size: imageView.frame.size,
    radius: 20.0
)

imageView.af_setImage(
    withURL: url,
    placeholderImage: placeholderImage,
    filter: filter,
    imageTransition: .crossDissolve(0.2)
)

GIF 처리

  • 별도로 없음 (직접 custom alamofire response serializer 를 만들도록 안내하고 있음)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment