[TIL] 2021.02.01

์Šน์•„ยท2021๋…„ 2์›” 1์ผ
0

[TIL] iOS WishList์•ฑ ๊ฐœ๋ฐœ ๐Ÿผ

๋ชฉ๋ก ๋ณด๊ธฐ
4/27

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ

  • Photos ํ”„๋ ˆ์ž„์›Œํฌ ( ๋ถ€์ŠคํŠธ ์ฝ”์Šค , ์ฐธ๊ณ  ์‚ฌ์ดํŠธ )
    - Photos ํ”„๋ ˆ์ž„์›Œํฌ๋Š” iOS ๋ฐ macOS์—์„œ ์‚ฌ์ง„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ์‚ฌ์ง„ ํ™•์žฅ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Photos ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ†ตํ•ด iOS ๋ฐ tvOS์—์„œ iCloud ์‚ฌ์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จํ•˜์—ฌ ์‚ฌ์ง„ ๋ฐ ๋น„๋””์˜ค์— ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด์— ํ‘œ์‹œ ๋ฐ ์žฌ์ƒํ•  ์—์…‹์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ด๋ฏธ์ง€ ๋˜๋Š” ๋น„๋””์˜ค๋ฅผ ํŽธ์ง‘ํ•˜๊ฑฐ๋‚˜ ์•จ๋ฒ”, ํŠน๋ณ„ํ•œ ์ˆœ๊ฐ„ ๋ฐ iCloud ๊ณต์œ  ์•จ๋ฒ”๊ณผ ๊ฐ™์€ ์—์…‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  1. Info.plist์—์„œ Privacy - Photo Library Usage Description ์ถ”๊ฐ€
  2. ์ ‘๊ทผ ํ—ˆ๊ฐ€ ์—ฌ๋ถ€ ํ™•์ธ
    let photoAurhorizationStatus = PHPhotoLibrary.authorizationStatus()
    
    switch photoAurhorizationStatus  {
    case .authorized :
        print("์ ‘๊ทผ ํ—ˆ๊ฐ€๋จ")
        self.requestCollection()
    case .denied :
        print("์ ‘๊ทผ ๋ถˆํ—ˆ")
    case .notDetermined:
        print("์•„์ง ์‘๋‹ตํ•˜์ง€ ์•Š์Œ")
        PHPhotoLibrary.requestAuthorization( { (status) in
            switch status {
            case .authorized:
                print("์‚ฌ์šฉ์ž๊ฐ€ ํ—ˆ์šฉํ•จ")
                self.requestCollection()
                DispatchQueue.main.async {
                    self.collectionView.reloadData()
                }
            case .denied:
                print("์‚ฌ์šฉ์ž๊ฐ€ ๋ถˆํ—ˆํ•จ")
            default: break
            }
        })
    case .restricted:
        print("์ ‘๊ทผ ์ œํ•œ")
    case .limited:
        print("์ ‘๊ทผ ์–ด์ฉŒ๊ตฌ")
    @unknown default:
        print("์ ‘๊ทผ ์˜ค๋ฅ˜")
    }
  1. Photos๋ฅผ import ํ•ด์ค€๋‹ค.
    import Photos 
  1. Asset๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ fetchResult์™€ thumbnail image๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ์œ„ํ•œ imageManager๋ฅผ ์„ ์–ธํ•ด์ค€๋‹ค.
    var fetchResult: PHFetchResult<PHAsset>!
    var imageManager: PHCachingImageManager = PHCachingImageManager()
  1. Asset์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
    let cameraRoll: PHFetchResult<PHAssetCollection> = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil)
    guard let cameraRollCollection = cameraRoll.firstObject else {
        return
    }
    
    let fetchOptions = PHFetchOptions()
    // ์ตœ์‹  ์ˆœ์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์˜ต์…˜
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
    // asset ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
    self.fetchResult = PHAsset.fetchAssets(in: cameraRollCollection, options: fetchOptions) 
  1. CollectionView ์—ฐ๊ฒฐ
extension PhotoLibraryViewController:  UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.fetchResult?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoLibraryCell", for: indexPath) as? PhotoLibraryCell else { return UICollectionViewCell() }
        
        let asset: PHAsset = fetchResult.object(at: indexPath.item)
        imageManager.requestImage(for: asset, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFill, options: nil, resultHandler: { image, info in
                cell.updateUI(image)
        })
        
        return cell
    }
}

โœ๐Ÿป ์˜ค๋Š˜์€..

UIImagePickerController๋ฅผ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ ์‚ฌ์ง„ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์„ ํƒํ•˜๊ณ  ์‹ถ์–ด collectionView์— asset๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” PhotoKit์œผ๋กœ ๋ณ€๊ฒฝํ•ด๋ณด์•˜๋‹ค. PhotoKit ๊ฒ€์ƒ‰ ์ค‘ PHPicker๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋Š”๋ฐ iOS 14์ด์ƒ๋ถ€ํ„ฐ ์“ธ ์ˆ˜ ์žˆ๋Š”๊ฒƒ ๋นผ๊ณค ์•„์ฃผ ์“ธ๋งŒํ•œ๊ฒƒ ๊ฐ™์•„ PHPicker๋กœ ๋ณ€๊ฒฝํ• ๊นŒ ๊ณ ๋ฏผ์ค‘์ด๋‹ค.. ๋‚ด์ผ์€ PHPicker์— ๋Œ€ํ•ด ์ข€ ๋” ๊ณต๋ถ€ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€