如何从照片库中获取图像?
Posted
技术标签:
【中文标题】如何从照片库中获取图像?【英文标题】:How do I fetch images from photo library? 【发布时间】:2016-09-27 14:39:29 【问题描述】:您好,我已经从 swift 2 切换到 swift 3。
这是我的uploadVC
class PhotoUploadVC: UICollectionViewController
fileprivate var assets: PHFetchResult<AnyObject>?
fileprivate var sideSize: CGFloat!
//是viewDidLoad()的sn-p
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?”
有人帮我吗?
【问题讨论】:
【参考方案1】:尝试改变:
fileprivate var assets: PHFetchResult<AnyObject>?
进入
var assets: [AnyObject]!
【讨论】:
以上是关于如何从照片库中获取图像?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 iOS 照片库中获取图像并在 UIWebView 中显示
iPhone:如何使用 iPhone 照片应用程序等“编辑”功能从照片库或相机中获取图像