在过去的几个版本的iOS上都可以工作,但在14上不能工作,我花了5天时间在谷歌上搜索。
每当我从图库中添加图像时,用于选择图像的弹出窗口都是空白的。
我已经尝试过UIImagePickerController(在iOS13及更低版本下运行良好)和新的PHPickerViewController。
在iOS14之前,用户单击一个栏按钮来“添加图像”,弹出窗口要求从图库中进行选择,然后向选取器显示图库中的图像。
我已经为UIImagePickerController设置了Info.plist“隐私-图片库使用说明”,我知道PHPickerViewController不需要烫发,因为它会为你获取图像。
我也经常看到这些紫色的警告,上面写着"xxxx只能在主线程中使用“,这在新的Xcode之前从未见过。
任何帮助都将不胜感激。运行Xcode12.0.1 (12A7300),模拟器运行iOS14。
亚当



class AddCustomExerciseViewController: UIViewController, UITextViewDelegate, UINavigationControllerDelegate, GADBannerViewDelegate, UIImagePickerControllerDelegate, PHPickerViewControllerDelegate{
let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in self.openGallery() }))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
func openGallery()
{
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
self.showGallery()
print("Access is granted by user")
case .notDetermined:
PHPhotoLibrary.requestAuthorization({
(newStatus) in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
self.showGallery()
print("success")
}
})
print("It is not determined until now")
case .restricted:
// same same
print("User do not have access to photo album.")
case .denied:
// same same
print("User has denied the permission.")
case .limited:
// same same
print("User has denied the permission.")
}
}
func showGallery()
{
if #available(iOS 14, *)
{
var configuration = PHPickerConfiguration()
configuration.filter = .images
configuration.selectionLimit = 0
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
else
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
else
{
let alert = UIAlertController(title: "Warning", message: "You don't have permission to access gallery.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
//get the file url
if let fileUrl = info[UIImagePickerControllerImageURL] as? URL {
print(fileUrl)
print(fileUrl.lastPathComponent)
let filePath = fileUrl.lastPathComponent
if(filePath.hasSuffix("GIF")||filePath.hasSuffix("gif"))
{
//to change later to support gifs
gifView.image = pickedImage
}
else
{
//to show the static image to users
gifView.image = pickedImage
}
//for saving the file name to retrieve in local parse datastore
imageNameToSave = fileUrl.lastPathComponent
}
//for saving the selected image
imageToSave = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
@available(iOS 14, *)
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
{
picker.dismiss(animated: true, completion: nil)
for result in results
{
result.itemProvider.loadObject(ofClass: UIImage.self, completionHandler: { (object, error) in
if let image = object as? UIImage {
DispatchQueue.main.async {
// Use UIImage
print("Selected image: \(image)")
}
}
})
}
} }发布于 2020-09-29 15:55:39
在iOS 14上,受限制的图像选择器权限对话框在一个单独的线程中打开,因此当授权返回时,您将需要在主线程上执行UI任务。
PHPhotoLibrary.requestAuthorization({
(newStatus) in
DispatchQueue.main.async {
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
self.showGallery()
print("success")
}
}
})发布于 2021-06-06 09:03:48
如果这对其他人有帮助的话,我发现编辑滚动视图外观代理背景会导致UIImagePickerController和PHPickerViewController中都有一个bug,导致它们无法在iOS 14和更高版本中显示图像。我已经编辑了滚动视图背景代理,因为它即使在灯光模式下也是黑色的。经过几个小时的谷歌搜索和测试,我发现删除scrollview代理上的外观背景修改可以让图像选择器按预期工作。我向苹果提交了一份错误报告。导致错误的代码行是:
UIScrollView.appearance().backgroundColor = .systemBackgroundhttps://stackoverflow.com/questions/64095308
复制相似问题