swift3phfetchoptions

How do I fetch images from photo library?


Hi I had switch from swift 2 to swift 3.

Here is my uploadVC

class PhotoUploadVC: UICollectionViewController {


    fileprivate var assets: PHFetchResult<AnyObject>?
    fileprivate var sideSize: CGFloat!

//It is snippet of viewDidLoad()

override func viewDidLoad() {
        super.viewDidLoad()


        //Navigation Config
        self.navigationItem.title = "CAMERA ROLL"




        if PHPhotoLibrary.authorizationStatus() == .authorized {
            reloadAssets()
        } else {
            PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
                if status == .authorized {
                    self.reloadAssets()
                } else {
                    self.showNeedAccessMessage()
                }
            })
        }



    }

//Here is function about fetch images

fileprivate func showNeedAccessMessage() {
        let alert = UIAlertController(title: "Image picker", message: "App need get access to photos", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction) -> Void in
            self.dismiss(animated: true, completion: nil)
        }))

        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction) -> Void in
            UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
        }))

        self.show(alert, sender: nil)
    }

    fileprivate func reloadAssets() {
//        activityIndicator.startAnimating()
        assets = nil

        collectionView?.reloadData()
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)



        collectionView?.reloadData()


//        activityIndicator.stopAnimating()
    }

enter image description here

I got an error message above.

Where is problem?

I think "fileprivate var assets: PHFetchResult?"

Anyone helping me?


Solution

  • Try changing:

    fileprivate var assets: PHFetchResult<AnyObject>?
    

    into

    var assets: [AnyObject]!