0

我有一个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()


}
4

1 回答 1

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()

于 2016-08-11T13:12:51.077 回答