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

时间:2016-08-11 12:34:20

标签: ios swift photos authority

我有一个从图库

加载图片的集合视图

第一次运行app时,它总是会崩溃,因为没有使用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 个答案:

答案 0 :(得分: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()

中写下此内容