如何从照片库中获取图像?

时间:2016-09-27 14:39:30

标签: swift3 phfetchoptions

您好我已经从swift 2切换到swift 3。

这是我的uploadVC

class PhotoUploadVC: UICollectionViewController {


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

//它是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()
                }
            })
        }



    }

//这是关于获取图像的功能

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

我收到了上面的错误消息。

问题在哪里?

我认为&#34; fileprivate var assets:PHFetchResult?&#34;

有人帮我吗?

1 个答案:

答案 0 :(得分:1)

尝试更改:

fileprivate var assets: PHFetchResult<AnyObject>?

var assets: [AnyObject]!