1.請求權限

PHPhotoLibrary.requestAuthorization

 

2.需要的資料型態

PHAsset.fetchAssetsWithMediaType

 

3.取得全部圖片

enumerateObjectsUsingBlock

 

 

Code:

        PHPhotoLibrary.requestAuthorization { [weak self] (status) in

            

            // UI Thread

            dispatch_async(dispatch_get_main_queue(), {

                

                let images = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)

                images.enumerateObjectsUsingBlock({  (obj, index, stop) in

                    

                    guard let weakSelf = self else {

                        return

                    }

                    

                    let item = ArrayCollectionViewCellViewModel()

                    item.assets = obj as? PHAsset

                    

                    weakSelf.cellViewModel.append(item)

                    

                })

            })

            

        }

 

 

參考資料:

https://github.com/shinobicontrols/iOS8-day-by-day

http://stackoverflow.com/questions/26065774/which-phassetcollection-to-use-for-saving-an-image

https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibraryChangeObserver_Protocol/

http://objccn.io/issue-21-4/

arrow
arrow
    全站熱搜

    小賢 發表在 痞客邦 留言(0) 人氣()