如何从照片库中获取图像?
How do I fetch images from photo library?
嗨,我已经从 swift 2 切换到 swift 3。
这是我上传的VC
class PhotoUploadVC: UICollectionViewController {
fileprivate var assets: PHFetchResult<AnyObject>?
fileprivate var sideSize: CGFloat!
//是viewDidLoad()的片段
override func viewDidLoad() {
super.viewDidLoad()
//Navigation Config
self.navigationItem.title = "CAMERA ROLL"
if PHPhotoLibrary.authorizationStatus() == .authorized {
reloadAssets()
} else {
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
if status == .authorized {
self.reloadAssets()
} else {
self.showNeedAccessMessage()
}
})
}
}
//这里是获取图片的函数
fileprivate func showNeedAccessMessage() {
let alert = UIAlertController(title: "Image picker", message: "App need get access to photos", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction) -> Void in
self.dismiss(animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction) -> Void in
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
}))
self.show(alert, sender: nil)
}
fileprivate func reloadAssets() {
// activityIndicator.startAnimating()
assets = nil
collectionView?.reloadData()
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
collectionView?.reloadData()
// activityIndicator.stopAnimating()
}
我收到上面的错误消息。
问题出在哪里?
我觉得"fileprivate var assets: PHFetchResult?"
有人帮我吗?
尝试更改:
fileprivate var assets: PHFetchResult<AnyObject>?
进入
var assets: [AnyObject]!
嗨,我已经从 swift 2 切换到 swift 3。
这是我上传的VC
class PhotoUploadVC: UICollectionViewController {
fileprivate var assets: PHFetchResult<AnyObject>?
fileprivate var sideSize: CGFloat!
//是viewDidLoad()的片段
override func viewDidLoad() {
super.viewDidLoad()
//Navigation Config
self.navigationItem.title = "CAMERA ROLL"
if PHPhotoLibrary.authorizationStatus() == .authorized {
reloadAssets()
} else {
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
if status == .authorized {
self.reloadAssets()
} else {
self.showNeedAccessMessage()
}
})
}
}
//这里是获取图片的函数
fileprivate func showNeedAccessMessage() {
let alert = UIAlertController(title: "Image picker", message: "App need get access to photos", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction) -> Void in
self.dismiss(animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction) -> Void in
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
}))
self.show(alert, sender: nil)
}
fileprivate func reloadAssets() {
// activityIndicator.startAnimating()
assets = nil
collectionView?.reloadData()
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
collectionView?.reloadData()
// activityIndicator.stopAnimating()
}
我收到上面的错误消息。
问题出在哪里?
我觉得"fileprivate var assets: PHFetchResult?"
有人帮我吗?
尝试更改:
fileprivate var assets: PHFetchResult<AnyObject>?
进入
var assets: [AnyObject]!