PhotoPicker 发现错误: 错误域 = PlugInKit 代码 = 13

我试图在 UIImageView 中显示来自照片库的图像

完整的错误是:

2017-06-09 21:55:59.063307+0200 firstapp2.0[12873:1120778] PhotoPicker 发现错误: 错误域 = PlugInKit 代码 = 13“查询取消” UserInfo = { NSLocalizedDescription = query  取消}

我的代码如下:

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)
}
}
73822 次浏览

您需要使用显式的 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)
}

这解决了 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]){}

确保在开头添加 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)
}

我也遇到了同样的问题。上面提到的建议对我都不管用。然而,我解决了它,通过明确呼吁采摘。解散和解散后,。我不清楚为什么我会做出这样的驳回电话,但它为我工作。

   @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!)
}


}

I found it! It is trying to tell you that you do not have authorization to "photos" You need to include the #import <Photos/Photos.h> and request authorization for example like this in Objective-C.

Hope this will save you some time. I spent two full days debugging this!

[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;
}
}];

我相信有人可以告诉你如何在斯威夫特做同样的事情。

当您在 Info.plist文件中使用 没有配置使用照片库的权限时,也会发生此错误。

您需要在 Info.plist中添加 Privacy - Photo Library Usage Description绳子,当您的应用程序第一次尝试访问用户的照片库时,将出现这个 绳子

Info.plist的最终调整应该是这样的: enter image description here

I read the all these answers and little bit modified here and not needed to dismiss, this worked for me, Thanks others who have replied here.

    @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
}
}

I fixed this issue, call function below into viewdidload or viewWillAppear or viewDidAppear to check permission for your app.

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

我也有同样的错误,我用这种方法修正了它:

以不同的方式获取图像对象取决于 UIImagePickerController 所允许的编辑是真还是假

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {


if isImagePickerAllowEditing {
sourceImage = info[UIImagePickerControllerEditedImage] as? UIImage
} else {
sourceImage = info[UIImagePickerControllerOriginalImage] as? UIImage
}


....


}

当然,如上所述,请先获得访问图片库的许可。

这解决了我的错误:

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)


}

我找到了图像 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)
}

I found this solution. We got this error due to these two reason which is mentioned below.

  1. 首先,我们需要调用这个方法进行授权

Authorization Code

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.")
}
}
  1. didFinishPickingMediaWithInfo方法调用的正确方法

错:

private func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
}

Right

@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
}

我希望这个解决方案将帮助您解决这个错误。

如果对你有用,不要忘了标记它是正确的,这样可以帮助别人找到正确的方法。

这可能不是最直接有用的答案-但希望它会有所帮助!我99% 肯定这个特定的错误消息实际上并没有指向真正的问题。我花了好几个小时为同样的问题揪头发。

1) I had authorization for photos printing to the console when I'd launch my picker, 2) I could navigate and pick a photo easy enough, and 3) when I would return me to my view upon dismissing the picker, my button image wasn't updated with the new photo... and the only hint that I had to the problem was the exact same error message that you are receiving.

原来我是用 ViewWillAppear 硬编码了一个占位符图像,而不是 ViewDidLoad。每次选择器解散时,它都会调用 ViewWillAppear 并用我的硬编码图像替换我选择的图像。我修复了这个问题,并且确信一切都正常运行... ... 每次从选择器返回时,我仍然看到那个错误消息。

I recommend looking somewhere other than the ImagePicker for your problem and you'll likely find it.

click on imageView to load image from photo library and show preview on same imageview. on 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)
}

在 Swift 3中缺少代表

对我来说,所有的设置都是正确的:
1-委托(UIImagePickerControllerRegiate,UINavigationControllerGenerate) ;
2-已核实的许可;
3-已经完成的清单;
在这个答案上读到的一切,我做到了;

But i forgot to implement pickerview.delegate = self on viewDidLoad 因为我复制和粘贴从我的另一个视图控制器和忘记!

我希望它能帮助别人,检查你的复制/粘贴第一! ! !

请按以下步骤检查:

  1. 导入照片
  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)
    })
    }
    }
    
  3. Present 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)
    }
    }
    
  4. The key step, make sure the delegate method is like following strictly

    // 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)
    }
    

尝试了一些组合反应,但没有多少成功。

使用 Swift 4,我发现我需要确保实现以下两个条目,以确保选中图像并将其放入选择器(注意在发现扩展时遇到的“[发现]错误:

错误域 = PlugInKit Code = 13“查询取消”UserInfo = { NSLocalizedDescription = query 取消}

message still displays in the console, but it does not prevent you from adding an image). Maybe this is a message that results in the picker being dismissed?

1) UIImagePickerController的委托是 (UIImagePickerControllerDelegate & UINavigationControllerDelegate)?,因此需要显式地添加 UINavigationControllerDelegate作为协议之一:

class ViewController:UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate { .... }.

2) Make sure that the info.plist has the Privacy - Photo library Usage Description key and String value set.

当然,您需要确保创建一个 UIImagePickerController并将其委托设置为 ViewDidLoad()中的 self:

class ViewController:UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let imagePickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePickerController.delegate = self
}
...
}

如果您还没有这样做,尝试 this

产品 > 方案 > 编辑方案 > 环境变量 OS_ACTIVITY_MODE: disable

这能解决我的问题。

这就是我所做的,它解决了我的问题。

  • Add@objecc
  • 加上内部
  • add _ before imagePicker
  • 确保您使用的是 AnyObject 而不是 AnyObject

Hope this helps!

确保两者都是子类化的: UIImagePickerControllerDelegate, UINavigationControllerDelegate

Also, remember to set the delegate:

let picker = UIImagePickerController()
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.delegate = self  //Don't forget this line!
self.present(picker, animated: true, completion: nil)

学分到 这个来源

这让我很困惑,但是下面的链接给出的答案对我很有用。错误消失,图像按预期显示

像上面的一个答案一样,在函数之前必须有 (_ picker...@objc

@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
...
}

Https://forums.developer.apple.com/thread/82105

我这边 UINavigationControllerDelegate代表不见了。

class YourViewController:UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate

在我的例子中,我得到了这个错误,因为我在没有首先验证用户是否允许访问图像的情况下就展示了图像选择器控制器。在我的代码的其他地方,用户已经授予了许可,所以我认为我需要做的就是导入照片。然而,由于他们“可能”没有授予许可,在应用程序添加这一点之前,我提出的图像选择器控制器为我解决了这个问题(导入照片以及)。

// request photos permission if permission has not been granted
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) in


})
}

如果这个答案似乎是多余的,我很抱歉,但我必须从几个不同的答案拼凑起来,让我的解决方案工作。也许这会对其他使用 Swift 4的人有所帮助。

对我有效的解决方案是直接进入 Product (在屏幕顶部)-> Scheme-> EditScheme-> Arguments

在 Environment Variables 中,添加 OS _ ACTIVITY _ MODE,其值为“ able”

希望这对别人有用!

I'll attach a screenshot just in case my description seemed confusingenter image description here

当我试图从选择器回调 imagePickerController中显示另一个控制器时,我得到了相同的消息。对我有效的解决方案是将我的代码移动到完成回调中,从选择器的眼中消除方法

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)
}
}
}

XCODE 10.1/SWIFT 4.2:

  1. 添加所需的权限(其他提到的权限)

  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)
    }
    

用这个密码

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

Swift 4.2Xcode 10.0上也有同样的问题。虽然图像选择器工作正常,但是 Xcode 在控制台上显示了这个错误。为了摆脱这个:

  • 在 Xcode 菜单产品 > 方案 > 编辑方案
  • Select 'Run' tab, then 'Arguments'
  • 在“ Environment Variables”部分添加一个名称为 操作系统 _ 活动 _ 模式且值为 关闭的变量

我花了很长时间才找到解决这个问题的方法。看起来问题在于找不到 UIImagePickerControllerControlate 的 image PickerController 函数。如果您正确设置了委托,并且它在升级到 Swift 4之后停止工作,那么问题可能是您没有通过使用@objecc 使它能够访问 Objective-C,这在 Swift 4之后是必需的。

It has to be:

@ objecc func imagePickerController (...

而不是

func imagePickerController(...

固定它设置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
})
}
}

使用 did FinishPickingMediaWithInfo

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)
}

这个“ Error Domain = PlugInKit Code = 13”错误让很多人感到困惑,尽管它看起来是一个无害的苹果调试消息。我在我的用户界面中设置图像时遇到了麻烦,我认为这个错误就是问题所在,不过这个问题有点微妙。

我将图像选择器控制器放在一个独立于视图控制器的文件中。

MyViewController 迅捷

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 并单独调用委托的 did Select 方法,那么将调用该方法,但是图像没有设置,因此请确保使用忽略补全。

同样,我知道这个答案可能与最初的问题没有直接关系,但我有一种感觉,很多人来这里是为了这个问题,而给出的解决方案并没有帮助。原因是,大多数给出答案的人有他们的图像选择器控制器在同一类作为他们的主视图控制器,因此不需要使用一个完成。

迅捷5及以上:

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
}






func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
dismiss(animated: true)
chosenImage = image
**pic.image = chosenImage**


}


@IBAction func tap(_ sender: Any) {
self.text.text = "Kreason"
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
//imagePicker.dataSource = self
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true)


}
}

在我看来,快速分配内存管理似乎是由苹果团队优化和修复的。 我的代码是完美的工作,直到一个迅速的更新,我发现我的“错误”是,我没有保留采摘器,但它已经工作了很长时间没有保留,所以这里有一个完全准备就绪的工作解决方案。

// 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>

还有

import Photos
class 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
}
}

尽量不要在方法中使用 UIImagePickerController,而是使用 它作为一个类变量,并且只创建一次。

当我忘记为 UIImagePickerController 设置一个委托时,我也遇到了同样的问题