Как проверить полномочия для доступа к фотографиям?

У меня есть коллекция, которая загружает изображения из галереи

При первом запуске приложения всегда происходит сбой, поскольку доступ не предоставляется для использования 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()


}

1 ответ

Решение
 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()

Другие вопросы по тегам