如何检查访问照片的权限?

How to check for authority to access photos?

我有一个 collectionview,用于从图库中加载图像

应用程序第一次运行时总是崩溃,因为没有授予使用 phtos 的权限。如何在代码中检查权限??

override func viewDidLoad()
{
    super.viewDidLoad()
    self.navigationController?.navigationBar.barTintColor = UIColor.grayColor()
    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)

    var i = 0
    repeat
    {
        if (collection.count > 0)
        {
            if let first_Obj:AnyObject = collection.objectAtIndex(i)
            {
                self.assetCollection = first_Obj as! PHAssetCollection
            }
            i += 1
        }
    }while( i < collection.count)




    // Do any additional setup after loading the view.
}

 override func viewWillAppear(animated: Bool)
{
    if let layout = self.photoAlbum!.collectionViewLayout as? UICollectionViewFlowLayout{
        let cellSize = layout.itemSize

        self.assetThumbnailSize = CGSizeMake(cellSize.width, cellSize.height)
    }

    //fetch the photos from collection
    self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
    self.photoAlbum!.reloadData()


}
 if ALAssetsLibrary.authorizationStatus() == ALAuthorizationStatus.NotDetermined {
    self.navigationController?.navigationBar.barTintColor = UIColor.grayColor()
    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)

    var i = 0
    repeat
    {
        if (collection.count > 0)
        {
            if let first_Obj:AnyObject = collection.objectAtIndex(i)
            {
                self.assetCollection = first_Obj as! PHAssetCollection
            }
            i += 1
        }
    }while( i < collection.count)




    // Do any additional setup after loading the view.
}

 override func viewWillAppear(animated: Bool)
{
    if let layout = self.photoAlbum!.collectionViewLayout as? UICollectionViewFlowLayout{
        let cellSize = layout.itemSize

        self.assetThumbnailSize = CGSizeMake(cellSize.width, cellSize.height)
    }

    //fetch the photos from collection
    self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
    self.photoAlbum!.reloadData()

    }else {
    self.navigationController?.navigationBar.barTintColor = UIColor.grayColor()
    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)

    var i = 0
    repeat
    {
        if (collection.count > 0)
        {
            if let first_Obj:AnyObject = collection.objectAtIndex(i)
            {
                self.assetCollection = first_Obj as! PHAssetCollection
            }
            i += 1
        }
    }while( i < collection.count)




    // Do any additional setup after loading the view.
}

 override func viewWillAppear(animated: Bool)
{
    if let layout = self.photoAlbum!.collectionViewLayout as? UICollectionViewFlowLayout{
        let cellSize = layout.itemSize

        self.assetThumbnailSize = CGSizeMake(cellSize.width, cellSize.height)
    }

    //fetch the photos from collection
    self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
    self.photoAlbum!.reloadData()

    }

在 viewWillAppear() 或 viewDidLoad() 中写这个