我正在尝试在UIImageView中显示照片库中的图像
完整错误是:
2017-06-09 21:55:59.063307 + 0200 firstapp2.0 [12873:1120778] PhotoPicker 发现错误:错误Domain = PlugInKit Code = 13“查询已取消” UserInfo = {NSLocalizedDescription = query canceled}
我的代码包含在下面:
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
@IBOutlet weak var pic: UIImageView!
@IBOutlet weak var text: UILabel!
var chosenImage : UIImage!
override func viewDidLoad() {
super.viewDidLoad()
pic.isUserInteractionEnabled = true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [AnyHashable: Any]) {
var chosenImage = info[UIImagePickerControllerEditedImage]
self.pic!.image = chosenImage as! UIImage
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
@IBAction func tap(_ sender: Any) {
self.text.text = "Kreason"
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
答案 0 :(得分:32)
您需要制作明确的Objective-C参考:@objc
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
答案 1 :(得分:16)
我找到了!它试图告诉您,您没有“照片”的授权您需要在Objective-C中包含#import <Photos/Photos.h>
并请求授权,例如此类。
希望这会为您节省一些时间。我整整花了两天时间调试这个!
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusAuthorized:
NSLog(@"PHAuthorizationStatusAuthorized");
break;
case PHAuthorizationStatusDenied:
NSLog(@"PHAuthorizationStatusDenied");
break;
case PHAuthorizationStatusNotDetermined:
NSLog(@"PHAuthorizationStatusNotDetermined");
break;
case PHAuthorizationStatusRestricted:
NSLog(@"PHAuthorizationStatusRestricted");
break;
}
}];
我相信有人可以告诉你如何在Swift中做同样的事情。
答案 2 :(得分:15)
我找到了这个解决方案。由于下面提到的这两个原因,我们得到了这个错误。
授权码
func checkPermission() {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus() switch photoAuthorizationStatus {
case .authorized: print("Access is granted by user")
case .notDetermined: PHPhotoLibrary.requestAuthorization({
(newStatus) in print("status is \(newStatus)") if newStatus == PHAuthorizationStatus.authorized { / do stuff here */ print("success") }
})
case .restricted: / print("User do not have access to photo album.")
case .denied: / print("User has denied the permission.")
}
}
didFinishPickingMediaWithInfo
<强> 错: 强>
private func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
}
右
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
}
我希望此解决方案可以帮助您解决此错误。
如果它对您有效,请不要忘记将其标记为正确,这样可以帮助其他人找到正确的方法。
答案 3 :(得分:13)
尝试了一些组合响应但没有取得多大成功。
使用Swift 4,我发现我需要确保实现以下两个项目以确保选择图像并将其放入选择器中(请注意发现扩展时遇到的&#34; [发现]错误:
错误域= PlugInKit代码= 13&#34;查询已取消&#34; UserInfo = {NSLocalizedDescription =查询已取消}&#34;
消息仍显示在控制台中,但不会阻止您添加图像)。也许这是一条消息导致选择器被解雇?
1)UIImagePickerController
的代理人是(UIImagePickerControllerDelegate & UINavigationControllerDelegate)?
,因此需要明确添加UINavigationControllerDelegate
作为协议之一:
class ViewController:UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate { .... }.
2)确保info.plist设置了Privacy - Photo library Usage Description
密钥和字符串值。
当然,您需要确保在UIImagePickerController
中创建self
并将其委托设置为ViewDidLoad()
:
class ViewController:UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let imagePickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePickerController.delegate = self
}
...
}
答案 4 :(得分:11)
我解决了这个问题,请将下面的函数调用到viewdidload或viewWillAppear或viewDidAppear中以检查您的应用的权限。
func checkPermission() {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
print("Access is granted by user")
case .notDetermined:
PHPhotoLibrary.requestAuthorization({
(newStatus) in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
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.")
}
}
要使用上述方法,请不要忘记在课程顶部添加import Photos
。
答案 5 :(得分:7)
XCODE 10.1 / SWIFT 4.2:
添加所需的权限(提到了其他权限)
实现此委托功能:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.imgView.contentMode = .scaleAspectFit
self.imgView.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
答案 6 :(得分:7)
*缺少DELEGATE *
就我而言,所有设置都是正确的:
1 - 代表(UIImagePickerControllerDelegate,UINavigationControllerDelegate);
2 - 已经验证的权限;
3 - 已完成Plist;
4 - 我在答案中读到了所有内容;
但我忘了在typedef
上实施pickerview.delegate = self
因为我从我的其他viewController中复制并粘贴而忘记了!
我希望它可以帮助某人,首先检查你的COPY / PASTE !!!
答案 7 :(得分:6)
答案 8 :(得分:6)
这可能不是最直接有用的答案 - 但希望它会有所帮助!我99%确定这个特定的错误消息实际上并没有指出你真正的问题。我花了好几个小时把头发拉出同样的问题。
1)当我启动我的选择器时,我有权将照片打印到控制台,2)我可以轻松导航并选择一张照片,以及3)当我解雇时,我会回到我的视野选择器,我的按钮图像没有使用新照片进行更新...而且我遇到问题的唯一提示是您收到的完全相同的错误消息。
原来我在ViewWillAppear而不是ViewDidLoad中对占位符图像进行了硬编码。每次选择器被解雇时,它都会调用ViewWillAppear并用我的硬编码图像替换我选择的图像。我已经解决了这个问题,而且确定 - 现在一切正常......我每次从选择器返回时都会看到错误信息。
我建议您在ImagePicker以外的其他地方查找问题,并且您可能会找到它。
答案 9 :(得分:4)
这解决了Swift 4中的问题:
更改以下代码
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {}
到此:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {}
Swift 4.2 update:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){}
答案 10 :(得分:4)
确保您继承了两个:UIImagePickerControllerDelegate, UINavigationControllerDelegate
。
另外,请记住设置委托:
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.delegate = self //Don't forget this line!
self.present(picker, animated: true, completion: nil)
致this source的信用。
答案 11 :(得分:3)
找到解决此问题的方法使我一辈子。看来问题在于找不到UIImagePickerControllerDelegate的函数imagePickerController。如果您已经正确设置了委托,并且在升级到Swift 4后它停止工作,则问题可能出在您没有使用@objc使它可以被Objective-C访问,而@objc是Swift 4以来必需的。
必须为:
@objc func imagePickerController(...
代替
func imagePickerController(...
答案 12 :(得分:3)
确保在开头添加internal func
,然后在括号后添加小写(_ picker: UIImagePickerController…
,然后从AnyObject
更改为Any
] ...
检查以下代码:
internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
userPhoto.image = pickedImage
}
self.dismiss(animated: true, completion: nil)
}
答案 13 :(得分:3)
当我试图从选择器回调imagePickerController
中呈现另一个控制器时,我得到了相同的消息。对我有用的解决方案是将我的代码从picker dismiss方法的完成回调中移出
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let cropController:CropViewController = CropViewController(croppingStyle: .circular, image: pickedImage)
cropController.delegate = self
self.present(cropController, animated: true, completion: nil)
}
}
}
答案 14 :(得分:3)
这让我很难过,但下面链接的答案对我有用。错误消失,图像按预期显示
与上述答案中的一个一样,您必须在此功能之前拥有(_ picker...
,但也需要@objc
。
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
...
}
答案 15 :(得分:2)
在设置图像时通过设置1秒延迟来解决该问题。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
picker.dismiss(animated: false, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.discussionImage.image = image
})
}
}
答案 16 :(得分:2)
使用此代码
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
答案 17 :(得分:2)
在我的情况下,我收到此错误是因为我在没有首先验证用户是否允许访问图像的情况下呈现图像选择器控制器。在我的代码中,用户已经授予了权限,所以我认为我需要做的就是导入照片。但是,因为他们可能会&#39;在我提交图像选择控制器解决问题之前,在应用程序中添加此权限尚未授予权限(导入照片也是如此)。
// request photos permission if permission has not been granted
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) in
})
}
如果这个答案看起来多余,我很抱歉,但我不得不从几个不同的答案拼凑起来让我的解决方案正常运作。也许这会帮助其他人使用Swift 4。
答案 18 :(得分:2)
这就是我所做的,它解决了我的问题。
希望这有帮助!
答案 19 :(得分:2)
请按以下步骤检查:
可以访问相册:
func authorizeToAlbum(completion:@escaping (Bool)->Void) {
if PHPhotoLibrary.authorizationStatus() != .authorized {
NSLog("Will request authorization")
PHPhotoLibrary.requestAuthorization({ (status) in
if status == .authorized {
DispatchQueue.main.async(execute: {
completion(true)
})
} else {
DispatchQueue.main.async(execute: {
completion(false)
})
}
})
} else {
DispatchQueue.main.async(execute: {
completion(true)
})
}
}
介绍UIImagePickerController
self.authorizeToAlbum { (authorized) in
if authorized == true {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = false
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
}
}
关键步骤,确保委托方法严格遵循
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.pickedImage = pickedImage
if let finalImage = handleImage(originalImage: self.pickedImage!, maskImage: self.maskImage!) {
self.imageView.image = finalImage
}
}
dismiss(animated: true, completion: nil)
}
答案 20 :(得分:2)
这解决了我的错误:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
addPhotoBtn.setImage(pickedImage, for: .normal)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
答案 21 :(得分:1)
当您未在Info.plist
文件中配置照片库使用权限时,也会出现错误。
您需要在Privacy - Photo Library Usage Description
中添加Info.plist
,其中字符串将在您的应用首次尝试访问用户的照片库时显示。
答案 22 :(得分:1)
在我的结尾#iso8601
委托失踪。
UINavigationControllerDelegate
答案 23 :(得分:1)
我有同样的错误,我以这种方式修复它:
以不同的方式获取图像对象取决于UIImagePickerController allowEditing是true还是false
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if isImagePickerAllowEditing {
sourceImage = info[UIImagePickerControllerEditedImage] as? UIImage
} else {
sourceImage = info[UIImagePickerControllerOriginalImage] as? UIImage
}
....
}
当然,请先获得访问照片库的权限,如上述评论。
答案 24 :(得分:0)
Swift 5及以上:
extern
答案 25 :(得分:0)
在我看来,苹果分配团队似乎已经优化并修复了快速分配内存管理问题。 在快速更新之前,我的代码可以正常工作,我发现我的“错误”是我没有保留选择器,但是由于没有保留它已经有很长时间了,因此这里提供了一个完整的随时可用的解决方案。
// Add Privacy - Photo Library Usage Description to your info.plist
<key>NSPhotoLibraryUsageDescription</key>
<string>APPNAME need to access your pictures to violate your privacy :P</string>
。
导入照片
类MyImageSelector:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var picker = UIImagePickerController()
// Make sure to retain this picker var until the picker returned
func checkPermission() {
let call = self.presentAuthorized
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
print("Access is granted by user")
showPicker()
case .notDetermined:
PHPhotoLibrary.requestAuthorization({ newStatus in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
print("success")
showPicker()
}
})
case .restricted:
print("User do not have access to photo album.")
case .denied:
print("User has denied the permission.")
}
}
func showPicker() {
picker = UIImagePickerController()
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.delegate = self //Don't forget this line!
self.present(picker, animated: true, completion: nil)
}
@objc public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// DO something with picture
}
@objc public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// User cancelled the pick
}
}
答案 26 :(得分:0)
此“错误域= PlugInKit代码= 13”错误使很多人感到困惑,尽管这似乎是对苹果无害的调试信息。我在选择要在用户界面中设置的图像时遇到了麻烦,并认为该错误是问题所在,但是有些微妙。
我将图像选择器控制器放在与呈现它的视图控制器不同的文件中。
MyViewController.swift
class MyViewController: UIViewController {
@IBOutlet weak var profileImage: UIImageView!
...
}
extension MyViewController: ImagePickerDelegate {
func didSelect(image: UIImage?) {
self.profileImage.image = image
}
}
ImagePicker.swift
open class ImagePicker: NSObject {
...
private func pickerController(_ controller: UIImagePickerController, didSelect image: UIImage?) {
controller.dismiss(animated: true, completion: {self.delegate?.didSelect(image: image)}
)
}
}
大多数教程和答案的完成度设置为零。但是,如果将补全设置为nil并分别调用委托的didSelect方法,则会调用该方法,但未设置图像,因此请确保使用dismiss补全。
同样,我知道这个答案可能与原始问题没有直接关系,但是我感觉很多人都来这里讨论这个问题,并且给定的解决方案也无济于事。原因是大多数给出答案的人将其图像选择器控制器与主视图控制器放在同一类中,因此不需要使用补全。
答案 27 :(得分:0)
我找到了IMage PickerView的解决方案......
@IBOutlet weak var myImageView: UIImageView!
var imagePicker = UIImagePickerController()
@IBAction func selectImage(_ sender: Any) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print(info)
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage
{
selectedImageFromPicker = editedImage as! UIImage
}else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{
selectedImageFromPicker = originalImage as! UIImage
}
dismiss(animated: true, completion: nil)
if var selectedImage = selectedImageFromPicker
{
myImageView.image = selectedImage
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
答案 28 :(得分:0)
使用didFinishPickingMediaWithInfo
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
self.dismiss(animated: true, completion: nil)
var chosenImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
userProfileImage.image = chosenImage
uploadImage(chosenImage)
}
答案 29 :(得分:0)
尽量不要使UIImagePickerController在方法内部,而要使 作为类变量,只能创建一次。
当我忘记为UIImagePickerController设置委托时,我遇到了与您相同的问题
答案 30 :(得分:0)
在 Swift 4.2 和 Xcode 10.0 上发生了相同的问题。尽管图像选择器工作正常,但Xcode在控制台上显示了此错误。要摆脱这种情况:
答案 31 :(得分:0)
我遇到了同样的问题。上述提议均不适合我。但是,我解决了它,通过详细说明调用picker.dismiss并解雇了帖子。我不清楚为什么我会这样解雇这些电话,但它对我有用。
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("Image picked")
picker.dismiss(animated: true, completion: nil)
dismiss(animated: true) {
print("dismissed")
self.delegate?.presentEditor(img: (info[UIImagePickerControllerOriginalImage] as? UIImage)!, id: self.id!)
}
}
答案 32 :(得分:0)
我阅读了所有这些答案,并在这里进行了一些修改,不需要解雇,这对我有用,感谢在这里回复的其他人。
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("########### media picked")
picker.dismiss(animated: true, completion: nil)
let img = (info[UIImagePickerControllerOriginalImage] as? UIImage)!
//set img -picked image to "profileImage" UIimageView
//use image here
self.profileImage.image = img
}
}
答案 33 :(得分:0)
点击imageView从照片库加载图片并在同一图片视图上显示预览。在swift 4
let imagePicker = UIImagePickerController()
@IBOutlet weak var userImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
let tap = UITapGestureRecognizer(target: self, action: #selector(SignUpViewController.click))
userImage.addGestureRecognizer(tap)
userImage.isUserInteractionEnabled = true
}
@objc func click()
{
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
userImage.contentMode = .scaleAspectFit
userImage.image = chosenImage
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
答案 34 :(得分:-1)