I'm trying to retrieve a PHAsset however PHAsset.fetchAssets(withALAssetURLs:options:) is deprecated from iOS 8 so how can I properly retrieve a PHAsset?
I had the same the issue, first check permissions and request access:
let status = PHPhotoLibrary.authorizationStatus()
if status == .notDetermined {
PHPhotoLibrary.requestAuthorization({status in
})
}
Just hook that up to whatever triggers your UIImagePickerController. The delegate call should now include the PHAsset in the userInfo.
guard let asset = info[UIImagePickerControllerPHAsset] as? PHAsset
Here is my solution:
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
}
}
}
I am not sure what you want.
Are you trying to target iOS 8?
This is how I fetch photos and it works in iOS (8.0 and later), macOS (10.11 and later), tvOS (10.0 and later). Code is commented where it may be confusing
- The first functions sets the options to fetch the photos
The second function will actually fetch them
//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() } } }) }) }
}
Edit based on OP's clarification
You need to set the delegate UIImagePickerControllerDelegate
then implement the following function
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
within said method, get the image like this:
var image : UIImage = info[UIImagePickerControllerEditedImage] as! UIImage
来源:https://stackoverflow.com/questions/44977292/how-to-retrieve-phasset-from-uiimagepickercontroller