๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UI

[iOS/UI] PHAsset์„ ๊ณ ํ™”์งˆ ์ด๋ฏธ์ง€๋กœ ๋ฐ”๊พธ๊ธฐ(feat.deliveryMode)

by Fomagran ๐Ÿ’ป 2021. 3. 31.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜ ์•Œ์•„๋ณผ๊ฑด PHAsset์œผ๋กœ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ UIImage๋กœ ๋ณ€ํ™˜ํ• ๋•Œ ๊ณ ํ™”์งˆ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Storyboard

 

๋จผ์ € ์Šคํ† ๋ฆฌ๋ณด๋“œ์— ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ด๋ฏธ์ง€๋ทฐ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ธํŒ…ํ•ฉ๋‹ˆ๋‹ค.

 


ViewController

 

์ด๋ฏธ์ง€๋ทฐ๋ฅผ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์— ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

  @IBOutlet weak var photo:UIImageView!

 

ํฌํ† ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์• ์…‹๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ allPhotos๋ฅผ ๋ฐ˜๋“ค์–ด์ค๋‹ˆ๋‹ค.

var allPhotos:PHFetchResult<PHAsset>? = nil

 

์• ์…‹๋ฐ์ดํ„ฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ ํฌํ† ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๊ถŒํ•œ์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

 

    func request() {
        PHPhotoLibrary.requestAuthorization { (status) in
            if status == .authorized {
                self.retrieveAsset()
            }
        }
    }

 

์•„๋ž˜์™€ ๊ฐ™์ด PHAsset์„ Image๋กœ ๋ฐ”๊ฟ” ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

 

 func assetToImage(asset: PHAsset) -> UIImage {
        var image = UIImage()
        let manager = PHImageManager.default()
        
        manager.requestImage(for: asset, targetSize: CGSize(width: 300, height: 300), contentMode: .aspectFill, options: nil, resultHandler: {(result, info)->Void in
            image = result!
        })
        return image
    }

 

๊ทธ ๋‹ค์Œ ์• ์…‹์„ ๋ฐ›์•„์™€ Image๋กœ ๋ฐ”๊พผ ๋’ค photo์˜ ์ด๋ฏธ์ง€๋ฅผ ๋ณ€ํ™˜ํ•œ ์ด๋ฏธ์ง€๋กœ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

 func retrieveAsset() {
        let fetchOptions = PHFetchOptions()
        allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        let image = assetToImage(asset: (allPhotos?.object(at: 0))!)
        
        DispatchQueue.main.async {
            self.photo.image = image
        }
       
    }

 

์ด๋ ‡๊ฒŒ ๋ฐ›์•„์˜ค๋ฉด ์•„์ฃผ ์ €ํ™”์งˆ์ธ ์‚ฌ์ง„์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค....

 


๊ณ ํ™”์งˆ๋กœ ๋ฐ›์•„์˜ค๊ธฐ

 

๋ฐ”๋กœ ์• ์…‹์„ ์ด๋ฏธ์ง€๋กœ ๋ฐ”๊ฟ€๋•Œ ์˜ต์…˜์„ ์ฃผ๋Š” ๊ฒƒ์ธ๋ฐ์š”!

 

PHImageRequestOptions๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ  ์—ฌ๊ธฐ์„œ deliveryMode๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

deliveryMode๋Š” ์ด๋ฏธ์ง€์˜ ํ€„๋ฆฌํ‹ฐ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๋”œ๋ฆฌ๋ฒ„๋ฆฌ๋ชจ๋“œ๋Š” 3๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ์š”.

 

1. fastFormat -> ๋น ๋ฅธ ๋กœ๋“œ์™€ ํ•จ๊ป˜ ์ €ํ™”์งˆ์˜ ์ด๋ฏธ๋ฅผ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค.

 

func assetToImage(asset: PHAsset) -> UIImage {
        var image = UIImage()
        let manager = PHImageManager.default()
        let options = PHImageRequestOptions()
        options.deliveryMode = .opportunistic
        
        manager.requestImage(for: asset, targetSize: CGSize(width: 300, height: 300), contentMode: .aspectFill, options: options, resultHandler: {(result, info)->Void in
            image = result!
        })
        return image
    }

 

๋”œ๋ฆฌ๋ฒ„๋ฆฌ ๋ชจ๋“œ ์ค‘ ๊ฐ€์žฅ ์ €ํ™”์งˆ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ชจ๋“œ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์œ„์—์„œ ๊ทธ๋ƒฅ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜ํ–ˆ์„๋•Œ๋ณด๋‹ค ํ›จ์”ฌ ๊น”๋”ํ•œ ์‚ฌ์ง„์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

2.opportunistic -> ์ด๋ฏธ์ง€ ํ’ˆ์งˆ์˜ ๊ท ํ˜•์„ ๋งž์ถ˜ ํ™”์งˆ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

  func assetToImage(asset: PHAsset) -> UIImage {
   	...
        options.deliveryMode = .opportunistic
    ...
    }
  

 

ํŒจ์ŠคํŠธํฌ๋งท๊ณผ ๋น„๊ตํ•ด๋ดค์„๋•Œ ๋น„์Šทํ•˜๊ธด ํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋” ์„ ๋ช…ํ•œ ๊ฐ์ด .... ์žˆ๋Š” ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค....

 

(์ œ ๋ˆˆ์— ๋˜‘๊ฐ™์ด ๋ณด์ด๋„ค์š”...)

3.highQualityFormat -> ๋กœ๋“œํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์— ๊ด€๊ณ„์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ตœ๊ณ  ํ’ˆ์งˆ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

  func assetToImage(asset: PHAsset) -> UIImage {
   	...
        options.deliveryMode = .highQulityFormat
    ...
    }
  

์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ํ•˜์ดํ€„๋ฆฌํ‹ฐ ์‚ฌ์ง„์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋กœ๋“œํ•˜๋Š” ์‹œ๊ฐ„์ด ์•ฝ๊ฐ„(?) ๊ฑธ๋ฆฐ ๊ฑฐ ๊ฐ™์•„์š”.

 

 


๋”œ๋ฆฌ๋ฒ„๋ฆฌ ๋ชจ๋“œ์—์„œ 3๊ฐ€์ง€ ๋ชจ๋“œ๋Š” ๊ฐ ๊ฐ ํฐ ์ฐจ์ด๋Š” ์žˆ์ง€ ์•Š์•„๋ณด์ž…๋‹ˆ๋‹ค.

 

๋‹ค๋งŒ ์• ์…‹์„ ์ด๋ฏธ์ง€๋กœ ๋ฐ”๊ฟ€๋•Œ ์•„๋ฌด๋Ÿฐ ์˜ต์…˜์„ ์ฃผ์ง€ ์•Š์•˜์„ ๋•Œ๋ณด๋‹จ ํ™•์—ฐํžˆ ์ฐจ์ด๋‚˜๋Š” ํ™”์งˆ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ˜น์‹œ๋ผ๋„ ๊ถ๊ธˆํ•˜์‹  ์ ์ด ์žˆ๊ฑฐ๋‚˜ ๊ฐœ์„ ํ•  ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€