Image Memory Size
2048x1536 픽셀의 590KB size의 이미지를 앱에서 로딩하면 얼마의 메모리가 필요할까?
- 약 14MB(2048x1536x4/1000000), 픽셀당 4바이트 가정
- 매우 많은 메모리 소비
Image Rendring Process
1. Load(iOS는 압축된 이미지를 가져와 590KB를 메모리에 로드)
2. Decode(GPU가 읽고 이해할 수 있도록 decode, 이 과정에서 10MB라는 큰 용량 차지)
Data Buffer -> ImageBuffer(Decoding을 통해 Data Buffer에 포함된 이미지 크기와 동일한 Image Buffer할당)
3. Render
Image Buffer -> Frame Buffer(ImageView를 실제 화면에 Display하는 과정에서 Rendering되는 영역)
실제로 큰픽셀(1920, 1280) 이미지 로딩할 때
vmmap으로 memory Size확인
약9.6MB를 이미지 처리에 사용중
Color Depth
SRGB에선 픽셀당 4바이트로 Red(1byte), Green(1byte), Blue(1byte), alpha포함하여 총 4바이트 필요
iOS에선 Wide format으로 넓은 color display로 렌더링 가능(wide format에선 픽셀당 2바이트가 필요, 두배로 늘림 8byte)
Single-color와 alpha를 사용하여 픽셀당 2byte만 사용 가능
UIGraphicsBeginImageContextWithOptions대신 UIGraphicsImageRenderer를 사용해라
UIGraphicsBeginImageContextWithOptions: 픽셀당 4byte차지
UIGraphicsImageRenderer: iOS12부터 자동으로 최적의 format 설정
UIGraphicsImageRender image(): Image(actions: )를 사용하여 이미지 렌더러로 이미지(UIImage)생성. Core Graphic Context를 작성하고 현재 Context로 설정
예시) UIGraphicsBeginImageContextWithOptions 픽셀당 4byte소요 비효율적
UIGraphicsImageRenderer 픽셀당 1byte로 최대 75% 적은 메모리 사용
Image Downsampling
UIImage는 원본 이미지를 압축하여 사용하기 때문에 내부 좌표 공간의 변화로 인해 성능이 떨어진다.
ImageIO를 사용하여 이미지를 Downsampling하여 메모리 사용량을 감소
- 기존 이미지 memory 사용량
- downsampling한 Image
- Image downsampling
'swift' 카테고리의 다른 글
Tabbar Paging With CollectionView (0) | 2022.01.25 |
---|---|
Carousel Effect (0) | 2022.01.22 |
ImageCache (0) | 2022.01.18 |
struct, class, enum (0) | 2021.12.14 |
URLSession (0) | 2021.12.10 |