gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:17:47 26 4
gpt4 key购买 nike

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

关于ios - 如何检查访问照片的权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38896552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com