์งง์€

[ ์งง์€ ] 1 - PHPhotoViewController

Forest Yun 2023. 8. 10. 23:03
728x90

 

๐Ÿ’๐Ÿป‍โ™€๏ธ'์งง์€' ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์‹œ์ž‘ํ•˜๋ฉฐ

๊ฐœ๋ฐœ์„ ํ•˜๋ฉฐ ๋งค์ผ ๋ฐฐ์šฐ๋Š” ๋‚ด์šฉ๋“ค์ด ๋งŽ์€๋ฐ ํ•ญ์ƒ ๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌํ•ด์•ผ์ง€ ํ•ด๋„ ๋ธ”๋กœ๊ทธ ์ •๋ฆฌ๋Š” ๊ณต๋ถ€์—์„œ ๊ตฌ์„ฑ๊นŒ์ง€ ๋ฉฐ์น ์ด ์†Œ์š”๋˜๊ธฐ์— ๋‹น์žฅ ๊ธ‰ํ•œ ํ”„๋กœ์ ํŠธ์— ๋ฐ€๋ ค ๊ฒฐ๊ตญ ์ฃผ์ œ๊ฐ€ ๊ธฐ์–ต์ด ๋‚˜์ง€ ์•Š์•„ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜๋‹ค. ์˜ค๋Š˜๋„ PhotoKit๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ์•Œ๊ฒŒ ๋œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ ํ”„๋กœ์ ํŠธ๊ฐ€ ๊ธ‰ํ•ด ๊ฒฐ๊ตญ์€ ํ•˜๋ฃจ๊ฐ€ ๋๋‚˜๋ฒ„๋ ธ๋‹ค. ํ•˜์ง€๋งŒ ๋”์ด์ƒ์€ ๋„˜์–ด๊ฐ€๊ณ  ์‹ถ์ง€ ์•Š์•„ 30๋ถ„๋งŒ์— ์ •๋ฆฌํ•˜๋Š” ์งง์€ ๊ธฐ๋ก์„ ํ•˜๊ธฐ๋กœ ๊ฒฐ์‹ฌํ–ˆ๋‹ค!

 

 

 

๐Ÿ“ธPHPickerViewController

์‚ฌ์šฉ์ž ๊ธฐ๊ธฐ์˜ ์•จ๋ฒ”์„ ์—ด์–ด ์‚ฌ์ง„์„ ์—…๋กœ๋“œํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์ •๋ง ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๋Š” ์นดํ†ก์˜ 'ํ”„๋กœํ•„ ์‚ฌ์ง„ ์„ค์ •'์ด ์žˆ๋‹ค. ์˜ค๋Š˜ ํ”„๋กœ์ ํŠธ์˜ ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ ์„ค์ •์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด PHPickerViewController๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” PhotoKit๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ViewController๋กœ, ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ UIImagePickerController์™€ ๋™์ผํ•˜๊ฒŒ ์•จ๋ฒ”์—์„œ ์‚ฌ์ง„์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ๋‹ค๋ฅธ ์ ์€ PHPickerViewController๋Š” ๊ฐœ์„ ๋œ ์•ˆ์ •์„ฑ๊ณผ ์‹ ๋ขฐ์„ฑ์„ ์ œ๊ณตํ•˜๊ณ , ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‚ฌ์ง„์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ๊ณผ Live Photo๋งŒ ์ œ๊ณตํ•  ์ˆ˜๋„ ์žˆ๋Š” ๋“ฑ ๊ฐœ๋ฐœ์ž์™€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ช‡๋ช‡ ์ด์ ์„ ์ œ๊ณตํ•œ๋‹ค. ๋˜ ์ ์  ํ™•์žฅ๋˜๊ณ  ์žˆ์–ด์„œ PHPickerViewController๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค.

 

 

PHPickerViewController | Apple Developer Documentation

A view controller that provides the user interface for choosing assets from the photo library.

developer.apple.com

 

 

 

 

๐Ÿ“‘itemProvide.loadObject(ofClass: completionHandler:)

์‚ฌ์ง„์„ ์„ ํƒํ•˜๊ณ  ๋‚˜๋ฉด View์— ๋ฐ˜์˜์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค! ๊ทผ๋ฐ ์ด๋ฏธ์ง€๋ฅผ ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์„๊นŒ? PHPickerViewControllerDelegate๋ฅผ ํ†ตํ•ด ์‚ฌ์ง„ ์„ ํƒ์ด ์™„๋ฃŒ๋œ ํ›„ ์ž‘์—…์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. didFinish ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์„ ํƒํ•œ ์‚ฌ์ง„ ์ •๋ณด๋ฅผ ๊บผ๋‚ด๋ณด์ž. ์„ ํƒ๋œ ์‚ฌ์ง„๋“ค์€ PHPickerResult ํƒ€์ž…์ธ results๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. PHPickerResult.identifier์€ ์‚ฌ์ง„์˜ id์ด๊ณ , PHPickerResult.itemProvider๋Š” ์‚ฌ์ง„์ด ๋‹ด๊ฒจ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. itemProvider์˜ ์—ฌ๋Ÿฌ load ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, loadObject(ofClass: completionHandler:)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ”๋กœ UIImage ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ View์— ๋ฐ˜์˜ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค! ๋งŒ์•ฝ url ํƒ€์ž…์„ ์›ํ•œ๋‹ค๋ฉด loadFileRepresentation์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์™ธ์—๋„ UTType์„ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทผ๋ฐ ์ด๊ฑด ์ข€ ๋‹ค๋ฃจ๊ธฐ ๊นŒ๋‹ค๋กœ์› ๋‹ค.. ์ผ๋‹จ UTType์„ ๊ฒ€์ƒ‰ํ•˜๋Š”๋ฐ ์˜›๋‚  ๊ณต์‹๋ฌธ์„œ๋ถ€ํ„ฐ ๋‚˜์™€์„œ ํ—ค๋งธ์—ˆ๋‹ค. ๊ทธ๋ž˜๋„ ์—ฌ๋Ÿฌ ํƒ€์ž…์„ ๊ฒฝํ—˜ํ•ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ธฐํšŒ์˜€๋‹ค๊ณ  ์ƒ๊ฐํ•ด์•ผ์ง€ ํœด^^

์‚ฌ์ง„์„ ๊ฐ€์ ธ์˜ค๋ฉด์„œ ์ •๋ง ๋‚˜๋ฅผ ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ ํ–ˆ๋˜ ๊ฑด ๋ฐ”๋กœ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์‚ฌ์ง„์„ ๋œปํ•˜๋Š” ๋ง์ด์—ˆ๋‹ค. Asset, Image, item, selection, result ๋“ฑ ๋ชจ๋‘๊ฐ€ ์•จ๋ฒ”์—์„œ ๊บผ๋‚ด์˜จ ์‚ฌ์ง„์„ ์˜๋ฏธํ•œ๋‹ค. ๋ฌผ๋ก  ํ•˜๋‚˜์”ฉ ๋”ฐ์ ธ๋ณด๋ฉด ๋‹ค๋ฅด๊ฒ ์ง€๋งŒ ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๋‚˜ํ•œํ…Œ๋Š” ์ผ๋‹จ ์ „์ฒด์ ์ธ ํฐ ํ‹€์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•˜๋‚˜๋กœ ํ†ต์ผํ•  ํ•„์š”๊ฐ€ ์žˆ์—ˆ๋‹ค. ์–ด์จŒ๋“  ์—ฌ๋Ÿฌ ๋ฒˆ ๋šซ์–ด์ง€๊ฒŒ ๊ณต์‹๋ฌธ์„œ์™€ Sample Code๋ฅผ ๋ณธ ๊ฒฐ๊ณผ ๋ชจ๋‘ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

PHPickerViewControllerDelegate | Apple Developer Documentation

A protocol the picker uses to communicate user selections.

developer.apple.com

 

Selecting Photos and Videos in iOS | Apple Developer Documentation

Improve the user experience of finding and selecting assets by using the Photos picker.

developer.apple.com

 

 

 

 

๐Ÿงš๐Ÿป‍โ™€๏ธ๋งˆ๋ฌด๋ฆฌ

30๋ถ„ ๋! ์‚ฌ์‹ค ์ฒซ๋‚ ์ด๊ธฐ๋„ ํ•˜๊ณ  ์ด๋ฏธ ํ”„๋กœ์ ํŠธ ํŒŒ์ผ๊ณผ ๊ณต์‹๋ฌธ์„œ๋“ค์„ ๋‹ซ์•„ ๋†“์€ ์ƒํƒœ๋ผ ๋‹ค์‹œ ์ฐพ์•„๋ด์•ผ ํ•ด์„œ 30๋ถ„์ด ํ›Œ์ฉ ๋„˜์—ˆ๋‹ค..

๋‹ค์Œ ์งง์€ ๊ธฐ๋ก ๋•Œ์—๋Š” ์ง„์งœ 30๋ถ„๋งŒ ์ •๋ฆฌํ•ด์„œ ์žฅ๊ธฐ์ ์œผ๋กœ ๊พธ์ค€ํžˆ ์ด์–ด๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ์ง€!

 

 

 

728x90