【发布时间】:2017-12-12 03:16:37
【问题描述】:
我正在尝试检索 PHAsset,但是 PHAsset.fetchAssets(withALAssetURLs:options:) 已从 iOS 8 中弃用,所以我怎样才能正确检索 PHAsset?
【问题讨论】:
标签: ios uiimage uiimagepickercontroller photos
我正在尝试检索 PHAsset,但是 PHAsset.fetchAssets(withALAssetURLs:options:) 已从 iOS 8 中弃用,所以我怎样才能正确检索 PHAsset?
【问题讨论】:
标签: ios uiimage uiimagepickercontroller photos
我有同样的问题,首先检查权限并请求访问:
let status = PHPhotoLibrary.authorizationStatus()
if status == .notDetermined {
PHPhotoLibrary.requestAuthorization({status in
})
}
只需将它连接到触发您的 UIImagePickerController 的任何东西。委托调用现在应该在 userInfo 中包含 PHAsset。
guard let asset = info[UIImagePickerControllerPHAsset] as? PHAsset
【讨论】:
nil。
UIImagePickerControllerPHAsset 替换为UIImagePickerController.InfoKey.phAsset,或仅替换为.phAsset。
这是我的解决方案:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if #available(iOS 11.0, *) {
let asset = info[UIImagePickerControllerPHAsset]
} else {
if let assetURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [assetURL], options: nil)
let asset = result.firstObject
}
}
}
【讨论】:
除非用户授权,否则 PHAsset 不会出现在 didFinishPickingMediaWithInfo: info 结果中,这对我来说并没有通过显示选择器来实现。我在 Coordinator init() 中添加了这个:
let status = PHPhotoLibrary.authorizationStatus()
if status == .notDetermined {
PHPhotoLibrary.requestAuthorization({status in
})
}
【讨论】:
我不确定你想要什么。
您是否尝试以 iOS 8 为目标?
这就是我获取照片的方式,它适用于 iOS(8.0 及更高版本)、macOS(10.11 及更高版本)、tvOS(10.0 及更高版本)。 代码在可能混淆的地方被注释了
第二个函数实际上会获取它们
//import the Photos framework
import Photos
//in these arrays I store my images and assets
var images = [UIImage]()
var assets = [PHAsset]()
fileprivate func setPhotoOptions() -> PHFetchOptions{
let fetchOptions = PHFetchOptions()
fetchOptions.fetchLimit = 15
let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
fetchOptions.sortDescriptors = [sortDescriptor]
return fetchOptions
}
fileprivate func fetchPhotos() {
let allPhotos = PHAsset.fetchAssets(with: .image, options: setPhotoOptions())
DispatchQueue.global(qos: .background).async {
allPhotos.enumerateObjects({ (asset, count, stop) in
let imageManager = PHImageManager.default()
let targetSize = CGSize(width: 200, height: 200)
let options = PHImageRequestOptions()
options.isSynchronous = true
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in
if let image = image {
self.images.append(image)
self.assets.append(asset)
}
if count == allPhotos.count - 1 {
DispatchQueue.main.async {
//basically, here you can do what you want
//(after you finish retrieving your assets)
//I am reloading my collection view
self.collectionView?.reloadData()
}
}
})
})
}
}
根据 OP 的说明进行编辑
需要设置委托UIImagePickerControllerDelegate
然后实现下面的函数
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
在上述方法中,得到这样的图像:
var image : UIImage = info[UIImagePickerControllerEditedImage] as! UIImage
【讨论】: