Bibi's DevLog ๐Ÿค“๐ŸŽ

PhotoKit ๊ทธ๋ฆฌ๊ณ  Photos์˜ ํด๋ž˜์Šค๋“ค ๋ณธ๋ฌธ

๐Ÿ“ฑ๐ŸŽ iOS/๐Ÿ Apple Developer Documentation

PhotoKit ๊ทธ๋ฆฌ๊ณ  Photos์˜ ํด๋ž˜์Šค๋“ค

๋น„๋น„ bibi 2022. 3. 24. 11:37

PhotoKit ๊ทธ๋ฆฌ๊ณ  Photos์˜ ํด๋ž˜์Šค๋“ค

https://zeddios.tistory.com/614 ๊ณผ ๊ทธ ์‹œ๋ฆฌ์ฆˆ๋“ค

Assets, Collections of Assets, List of Collections

๊ณตํ†ต์  : read-only, immutable, ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋งŒ ํฌํ•จ

  • Assets : ์‚ฌ์ง„, ๋น„๋””์˜ค, ๋ผ์ด๋ธŒ ํฌํ† 
  • Collections of Assets : ์•จ๋ฒ” ๋˜๋Š” Moments
  • List of Collections : ์•จ๋ฒ” ํด๋” ๋˜๋Š” Moment ํด๋Ÿฌ์Šคํ„ฐ

PhotoKit

: Photos ํ”„๋ ˆ์ž„์›Œํฌ์™€ PhotosUI ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ.

์‚ฌ์ง„ ์•ฑ์—์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘œ์‹œ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ชจ๋ธ๋งํ•˜๋Š” ์—”ํ‹ฐํ‹ฐ ๊ทธ๋ž˜ํ”„๋ฅผ ์ •์˜ํ•œ๋‹ค - lightweight, immutable

  • ์‚ฌ์ง„ ์•ฑ์˜ ์‚ฌ์ง„๋“ค, ๋น„๋””์˜ค๋“ค, ์•จ๋ฒ”์— ์ ‘๊ทผํ•ด ํ•ด๋‹น assets๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.
  • ์‚ฌ์ง„ ์•ฑ์˜ ์‚ฌ์ง„ ํŽธ์ง‘ ํ™•์žฅ ๊ธฐ๋Šฅ ์ง€์›
  • ํ‘œ์‹œ ๋ฐ ์žฌ์ƒ์„ ์œ„ํ•ด, ์‚ฌ์ง„ ์•ฑ์˜ ์‚ฌ์ง„ ๋ฐ ๋™์˜์ƒ assets์— ์ง์ ‘ ์ ‘๊ทผ ๋ฐ ์บ์‹ฑ ๊ฐ€๋Šฅ

PHObject

: Photo ๋ชจ๋ธ ๊ฐ์ฒด (assets์™€ collections)์˜ ์ƒ์œ„ ์ถ”์ƒ ํด๋ž˜์Šค.

๋ชจ๋“  PhotoKit ๊ฐ์ฒด๋Š” PHObject๋ผ๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค์—์„œ ์ƒ์†๋œ๋‹ค.

  • ์ด ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ๋งŒ๋“ค๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. ๊ตฌ์ฒด์ ์ธ ํ•˜์œ„ํด๋ž˜์Šค์ธ PHAsset, PHAssetCollection, PHCollectionList, PHObjectPlaceHolder์˜ ์ธ์Šคํ„ด์Šค๋กœ ์ž‘์—…ํ•ด์•ผ ํ•œ๋‹ค.
  • isEqual(_:) ๋ฐ ํ•ด์‹œ ๋ฉ”์„œ๋“œ
    • localIdentifier ํ”„๋กœํผํ‹ฐ์— ๋”ฐ๋ผ ๊ตฌํ˜„
    • asset ๋ฐ collection ๊ฐ์ฒด๋ฅผ ์ถ”์ ํ•  ์ˆ˜ ์žˆ์Œ

PHAsset : PHObject

: Photo Library์— ์žˆ๋Š” ์ด๋ฏธ์ง€, ๋น„๋””์˜ค, ๋ผ์ด๋ธŒ ํฌํ† ์— ๋Œ€ํ•œ ํ‘œ์ƒ.

์‚ฌ์šฉ์ž์˜ ์‚ฌ์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” "ํ•˜๋‚˜"์˜ asset(์ด๋ฏธ์ง€/๋น„๋””์˜ค/๋ผ์ด๋ธŒ ํฌํ† )์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ํ•ด๋‹น asset์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

  • PHObject์˜ ํ•˜์œ„ ํด๋ž˜์Šค
  • Asset๋ฅผ ๊ฐ€์ ธ์™€ ์ž‘์—…์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค - ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ ๋˜๋Š” PHAsset์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
  • Asset์€ metadata๋งŒ ํฌํ•จํ•œ๋‹ค.
  • Photos ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๊ฐ asset์˜ ์ธ๋„ค์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค - PHImageManager ์ฐธ๊ณ 
  • Asset ๊ฐ์ฒด๋Š” immutableํ•˜๋‹ค - Asset์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ํŽธ์ง‘ํ•˜๋ ค๋ฉด, PHAssetChangeRequest(๋ณ€๊ฒฝ ์š”์ฒญ ๊ฐ์ฒด) ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๊ทธ ์š”์ฒญ๋Œ€๋กœ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•œ๋‹ค. - PHPhotoLibrary ์ฐธ๊ณ 
  • fetch ๋ฉ”์„œ๋“œ๋“ค๋กœ ์‚ฌ์ง„ ์•ฑ์˜ ์ด๋ฏธ์ง€๋“ค์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
    • PHAsset.fetchAssets() : ๋ชจ๋“  ์‚ฌ์ง„์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ด

PHCollection

: Photo Asset Collection๊ณผ Collection List๋ฅผ ์œ„ํ•œ ์ƒ์œ„ ์ถ”์ƒํด๋ž˜์Šค.

  • PHObject์™€ ๊ฐ™์ด, PHCollection์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ๋งŒ๋“ค๊ฑฐ๋‚˜ ์ง์ ‘ ์ž‘์—…ํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค.
    • ๋Œ€์‹  PHAssetCollection ์ด๋‚˜ PHCollectionList๋ผ๋Š” ํ•˜์œ„ ๋ฆฌ์ŠคํŠธ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

โ€ผ๏ธ ์‚ฌ์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ ‘๊ทผ/์ˆ˜์ •ํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž์˜ ๋ช…์‹œ์ ์ธ ์Šน์ธ์ด ํ•„์š”ํ•˜๋‹ค. Fetching Collections ์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์‚ฌ์ง„ ์•ฑ์ด ์ž๋™์œผ๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ ์Šน์ธ ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•œ๋‹ค. (๋˜๋Š” PHPhotoLibrary.requestAuthorization(_:) ๋ฉ”์„œ๋“œ๋กœ ์›ํ•˜๋Š” ์‹œ๊ฐ„์— ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.)

์•ฑ์˜ info.plist ํŒŒ์ผ์—์„œ, ์•ฑ์ด ์‚ฌ์ง„ ์ ‘๊ทผ์„ ์š”์ฒญํ•˜๋Š” ์ด์œ ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์„ค๋ช…ํ•˜๋Š” NSPhotoLibraryUsageDescription ๊ฐ’์„ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด iOS 10.0+ ์— ์—ฐ๊ฒฐ๋œ ์•ฑ์ด ์ค‘๋‹จ๋œ๋‹ค

PHAssetCollection : PHCollection

: ์‚ฌ์šฉ์ž ์ œ์ž‘ ์•จ๋ฒ”, ์Šค๋งˆํŠธ ์•จ๋ฒ”, Moment์™€ ๊ฐ™์€ Photo Asset ๊ทธ๋ฃน์˜ ํ‘œ์ƒ.

(์‚ฌ์ง„ ๋˜๋Š” ๋น„๋””์˜ค Asset์˜ collection)

PHAsset์˜ Collection์ด๋ฉฐ, PHCollection์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ด๋‹ค.

  • ํ•˜๋‚˜์˜ Asset Collection์€ ์‚ฌ์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์•จ๋ฒ”์ด๋‚˜, moment์ด๋‚˜, ์Šค๋งˆํŠธ ์•จ๋ฒ” ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
  • asset collections์˜ ๋ฉค๋ฒ„๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ ค๋ฉด fetchAssets (: options :)์™€ ๊ฐ™์€ PHAssetํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • asset collections์„ ์ฐพ์œผ๋ ค๋ฉด Fetching Asset Collections์— ๋‚˜์—ด๋œ ๋ฉ”์†Œ๋“œ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • PHAsset๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ PHAssetCollection๋„ immutable ํ•˜๋‹ค - PHAssetCollectionChangeRequest๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์ƒ์„ฑ/๋ณ€๊ฒฝ/์‚ญ์ œ ์š”์ฒญ์„ ํ•ด์•ผ ํ•œ๋‹ค.
    • ์ž์„ธํ•œ ๋‚ด์šฉ์€ PHPhotoLibrary ์ฐธ๊ณ .

PHCollectionList : PHCollection

: Photo Asset Collection์„ ๋‹ด๊ณ  ์žˆ๋Š” ๊ทธ๋ฃน.

  • PHAssetCollection์˜ ๋ชจ์Œ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.
  • ์—ญ์‹œ immutable ํ•˜๋ฏ€๋กœ ์ƒ์„ฑ/๋ณ€๊ฒฝ/์‚ญ์ œ ์ž‘์—…์„ ์œ„ํ•ด์„œ๋Š” PHCollectionListChangeRequest๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.
    • ์ž์„ธํ•œ ๋‚ด์šฉ์€ PHPhotoLibrary๋ฅผ ์ฐธ๊ณ .

PHPhotoLibrary : NSObject

: ์‚ฌ์šฉ์ž์˜ ๊ณต์œ  ์‚ฌ์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(shared photo library)์— ๋Œ€ํ•œ ์ ‘๊ทผ ๋ฐ ๋ณ€๊ฒฝ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ณต์œ  ๊ฐ์ฒด(shared object).

  • shared PHPhotoLibrary ๊ฐ์ฒด๋Š” ์‚ฌ์ง„ ์•ฑ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ์ „์ฒด Asset ๋ฐ Collection ์ง‘ํ•ฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • ์ด ๊ฐ์ฒด๋Š” ๋‹ค์Œ ์ž‘์—…์— ์‚ฌ์šฉ๋œ๋‹ค:
    • ์•ฑ์ด ์‚ฌ์ง„ ์ปจํ…์ธ ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์šฉ์ž ๊ถŒํ•œ์„ ๊ฒ€์ƒ‰ ๋˜๋Š” ํ™•์ธ
    • Asset ๋ฐ Collection์„ ๋ณ€๊ฒฝํ•จ - asset ๋ฉ”ํƒ€ํ…Œ์ดํ„ฐ ๋˜๋Š” ์ปจํ…์ธ  ํŽธ์ง‘, ์ƒˆ asset ์‚ฝ์ž…, collection ์š”์†Œ ์žฌ๋ฐฐ์น˜...
    • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ณ€๊ฒฝ๋  ๋•Œ ์‹œ์Šคํ…œ์ด ๋ณด๋‚ด๋Š” ์—…๋ฐ์ดํŠธ ๋ฉ”์‹œ์ง€ ๋“ฑ๋ก

PHFetchResult : NSObject

: Photos fetch ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ๋ฐ˜ํ™˜๋œ Assets ๋˜๋Š” Collections์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ.

์‚ฌ์ง„ ์•ฑ์—์„œ ์‚ฌ์ง„ ๋˜๋Š” ์•จ๋ฒ”์„ ๊ฐ€์ ธ์˜ค๋ฉด(fetch), ๊ทธ ๊ฒฐ๊ณผ๋ฌผ๋“ค์ด PHFetchResult์— ๋‹ด๊ธด๋‹ค.

  • ์—…๋ฐ์ดํŠธ๋œ ์ปจํ…์ธ ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด, shared PHPhotoLibrary ๊ฐ์ฒด์— change observer๋ฅผ ๋“ฑ๋กํ•ด์•ผ ํ•œ๋‹ค.

PHAsset, PHCollection, PHAssetCollection, PHCollectionList ๋“ฑ์˜ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ฉด, Photos๋Š” ๊ฒฐ๊ณผ ๊ฐ์ฒด๋ฅผ fetch์˜ ๊ฒฐ๊ณผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Fetch Result์˜ ๋‚ด์šฉ์€ NSArray์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ ๋ฐ ์ปจ๋ฒค์…˜์„ ์‚ฌ์šฉํ•ด ์ ‘๊ทผํ•œ๋‹ค.

PHFetchResult ๊ฐ์ฒด๋Š” ํ•„์š”์— ๋”ฐ๋ผ Photos ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋‚ด์šฉ์„ ๋™์ ์œผ๋กœ ๋กœ๋“œํ•˜๋ฏ€๋กœ, ๋งŽ์€ ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ๋„ ์ตœ์ ์˜ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

Fetch result๋Š” ๋‚ด์šฉ์— ๋Œ€ํ•œ thread-safeํ•œ ์ ‘๊ทผ์„ ์ œ๊ณตํ•œ๋‹ค. fetch ํ›„์— fetch count๋Š” ์ผ์ •ํ•˜๋ฉฐ, fetch result์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ๋™์ผํ•œ localIdentifier๋ฅผ ์œ ์ง€ํ•œ๋‹ค.

Fetch result๋Š” ์ปจํ…์ธ ๋ฅผ ์บ์‹œํ•˜๊ณ , ๊ฐ์ฒด ํ•œ ๋ฌถ์Œ(batch)์„ ๊ฐ€์žฅ ์ตœ๊ทผ์— ์•ก์„ธ์Šคํ•œ ์ธ๋ฑ์Šค ์ฃผ์œ„์— ์œ ์ง€ํ•œ๋‹ค. batch ์™ธ๋ถ€์˜ ๊ฐ์ฒด๋Š” ๋” ์ด์ƒ ์บ์‹œ๋˜์ง€ ์•Š์œผ๋ฉฐ, ์ ‘๊ทผํ•˜๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜จ๋‹ค. ์ด ๋•Œ ํ•ด๋‹น ๊ฐ์ฒด์—์„œ ์ด์ „์— ์ฝ์€ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค.

PHFetchOptions : NSObject

: Asset๋‚˜ collection ๊ฐ์ฒด๋ฅผ fetchํ•  ๋•Œ Photos๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฐ๊ณผ(result)์˜ ํ•„ํ„ฐ๋ง, ์ •๋ ฌ, ๊ด€๋ฆฌ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์˜ต์…˜์˜ ์ง‘ํ•ฉ.

  • PHAsset, PHCollection, PHAssetCollection ๋ฐ PHCollectionList ํด๋ž˜์Šค์—์„œ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ assets ๋˜๋Š” collection์„ fetchํ•˜๋ฉด, ์š”์ฒญํ•œ ๊ฐ์ฒด๊ฐ€ ํฌํ•จ๋œ PHFetchResult๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ง€์ •ํ•œ ์˜ต์…˜์— ๋”ฐ๋ผ fetch result์— ํฌํ•จ๋œ ๊ฐ์ฒด, ์ •๋ ฌ๋ฐฉ๋ฒ•, fetch result์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์•ฑ์— ์•Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์–ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Photos๋Š” ์ œํ•œ๋œ key set์„ ์ œ๊ณตํ•˜๋ฉฐ, ํด๋ž˜์Šค์— ๋”ฐ๋ผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ key set๊ฐ€ ๋‹ค๋ฅด๋‹ค - ๊ณต์‹๋ฌธ์„œ ์ฐธ๊ณ .

PHAsset์€ ๊ทธ ์ž์ฒด๋กœ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ–๋Š” ๊ฒŒ ์•„๋‹Œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์ผ ๋ฟ์ด๋ฏ€๋กœ, Image Manager์—๊ฒŒ PHAsset๊ณผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ ์‹ค์ œ ์ด๋ฏธ์ง€๋ฅผ ์š”์ฒญํ•ด์•ผ ํ•œ๋‹ค.

Image Manager๋Š” PHImageManager, PHCachingImageManger์˜ 2 ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

PHImageManager : NSObject

: ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ธ๋„ค์ผ๊ณผ Asset ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ๋ฐ ์ƒ์„ฑ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•ด ์ฃผ๋Š” ๊ฐ์ฒด.

๊ธฐ๋Šฅ

  • full-size ์‚ฌ์ง„ Asset,
  • ์ธ๋„ค์ผ ์ด๋ฏธ์ง€,
  • ๋น„๋””์˜ค Asset์„ ์žฌ์ƒ/์ „์†กํ•˜๊ธฐ ์œ„ํ•œ AVFoundation ๊ฐ์ฒด ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณต.

์‚ฌ์šฉ๋ฒ•

์‚ฌ์ง„ / ๋น„๋””์˜ค ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด

  1. PHAsset ํด๋ž˜์Šค๋กœ ๊ด€์‹ฌ ์žˆ๋Š” Asset ๊ฐ€์ ธ์˜ค๊ธฐ
  2. default() ํ˜ธ์ถœํ•ด shared image manager ๊ฐ์ฒด ๊ฒ€์ƒ‰
  3. Asset์˜ ์‚ฌ์ง„/๋น„๋””์˜ค ๋ฐ์ดํ„ฐ ๋กœ๋“œ๋ฅผ ์œ„ํ•ด, Requesting Images ๋ฐ Requesting Video Objects์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ
  • PHImageManager๋Š” ์ œ๊ณตํ•˜๋Š” asset ์ด๋ฏธ์ง€์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์บ์‹œํ•œ๋‹ค.
    • ์œ ์‚ฌํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋™์ผ asset์— ๋Œ€ํ•ด ๋‹ค์‹œ ์š”์ฒญํ•˜๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๋” ๋นจ๋ฆฌ ๋ฐ˜ํ™˜ํ•จ
  • ์—ฌ๋Ÿฌ asset์˜ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ๋กœ๋“œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, PHCachingImageManager๋ฅผ ์‚ฌ์šฉํ•ด ํ•„์š”ํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋“œํ•˜์—ฌ ์บ์‹œ๋ฅผ ์ค€๋น„ํ•ด๋ผ.

PHCachingImageManager : PHImageManager

: ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ธ๋„ค์ผ์˜ ๊ฒ€์ƒ‰ ๋˜๋Š” ์ƒ์„ฑ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๋ฉฐ, ๋งŽ์€ ์ˆ˜์˜ Asset์„ ์‚ฌ์ „์— ์ผ๊ด„ ๋กœ๋“œํ•˜๋Š” ๋ฐ ์ตœ์ ํ™”๋˜์–ด ์žˆ๋Š” ๊ฐ์ฒด.

  • ๋งŽ์€ Asset ์ž‘์—…์„ ํ•  ๋•Œ์˜ ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ์œ„ํ•ด, ์ถ”ํ›„ ๊ฐœ๋ณ„ ์ด๋ฏธ์ง€๋ฅผ ์š”์ฒญํ•  ๋•Œ ์ง€์—ฐ๋˜์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด asset image๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์ค€๋น„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์ง„/๋น„๋””์˜ค asset์˜ ์ธ๋„ค์ผ๋กœ collectionView ๋“ฑ์˜ UI๋ฅผ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ.
  • PHCachingImageManager์˜ ์ฃผ์š” ๊ธฐ๋Šฅ์€, ๋Œ€๋ถ€๋ถ„ ์ƒ์œ„ ํด๋ž˜์Šค์ธ PHImageManager์— ์˜ํ•ด ์ •์˜๋˜์–ด ์žˆ๋‹ค.

์‚ฌ์šฉ๋ฒ•

  1. PHCachingImageManager์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  2. PHAssetํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ด€์‹ฌ์žˆ๋Š” asset์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  3. asset์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€๋ฅผ ์ค€๋น„ํ•˜๋ ค๋ฉด, ์ถ”ํ›„ ๊ฐœ๋ณ„ asset์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€๋ฅผ ์š”์ฒญ ํ•  ๋•Œ ์‚ฌ์šฉ ํ•  target size, content mode ๋ฐ ์˜ต์…˜๊ณผ ํ•จ๊ป˜startCachingImages(for:targetSize:contentMode:options:)๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์„ธ์š”.
  4. ๊ฐœ๋ณ„ asset์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ requestImage(for:targetSize:contentMode:options:resultHandler:) ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ํ•ด๋‹น asset์„ ์ค€๋น„ํ•  ๋•Œ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  5. ์š”์ฒญํ•œ ์ด๋ฏธ์ง€๊ฐ€ ์ด๋ฏธ ์ค€๋น„๋œ ์ด๋ฏธ์ง€ ์ค‘ ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ, PHCachingImageManager๊ฐ์ฒด๋Š” ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ์ฆ‰์‹œ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด Photos๊ฐ€ ํ•„์š”์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€๋ฅผ ์ค€๋น„ํ•˜๊ณ , ๋‚˜์ค‘์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์บ์‹ฑํ•ฉ๋‹ˆ๋‹ค.

์ด์™ธ์— startCachingImages, stopCachingImages ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๋„ ์ œ๊ณตํ•œ๋‹ค.


์˜ˆ์ œ ์ฝ”๋“œ

:


import Photos
import UIKit

class PhotoManager: NSObject {
    enum NotificationNames {
        static let didChangePhotos = Notification.Name("PhotoManagerDidChangePhotos")
    }

    private var photos: PHFetchResult<PHAsset>

    var count: Int {
        return self.photos.count
    }

    override init() {
        self.photos = PHAsset.fetchAssets(with: .image, options: nil)
        super.init()

        switch PHPhotoLibrary.authorizationStatus() {
            case .authorized:
                PHPhotoLibrary.shared().register(self)
            default:
                return
        }
    }

    func makePhotoData(index: Int, size: CGSize) -> UIImage? {
        let asset = self.photos.object(at: index)
        var photo: UIImage?

        PHCachingImageManager().requestImage(for: asset, targetSize: size, contentMode: .aspectFit, options: nil) { (image, _) in
            photo = image
        }
        return photo
    }
}

extension PhotoManager: PHPhotoLibraryChangeObserver {
    func photoLibraryDidChange(_ changeInstance: PHChange) {

        let previousPhotos = self.photos
        let changedPhotos = changeInstance.changeDetails(for: previousPhotos)
        self.photos = changedPhotos?.fetchResultAfterChanges ?? previousPhotos

        NotificationCenter.default.post(name: NotificationNames.didChangePhotos, object: self)
    }
}