我无法将照片库中的图像设置到 imageView,并出现错误 Error Domain=PlugInKit Code=13 “query cancelled”
I can't set image from photo library to imageView, and got error Error Domain=PlugInKit Code=13 “query cancelled”
我正在使用 swift4 开发一个 ios 应用程序。
点击profile image view时,在imageview上设置profile image,我有点击手势调用功能。
下面是我的代码。
我按照 swift 指南使用扩展实现了选择器委托。
看到几个关于同一个问题的文档,我想我做了我必须做的。但我无法找出错误,并在 imageview 上设置图像。
请检查我的代码。
import UIKit
import Kingfisher
import Alamofire
import Photos
class MyProfileVC: BaseNavigationVC {
@IBOutlet weak var stateMessageLabel: UILabel!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var nicknameLabel: UILabel!
@IBOutlet weak var sexImageView: UIImageView!
@IBOutlet weak var hash1Button: UIButton!
@IBOutlet weak var hash2Button: UIButton!
@IBOutlet weak var hash3Button: UIButton!
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tappedProfileImageView))
profileImageView.addGestureRecognizer(tap)
profileImageView.isUserInteractionEnabled = true
}
@objc func tappedProfileImageView(){
openGallery()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(false)
self.checkPermission()
//I updated this part. this code works correctly
if let imageUrl = UserInfo.shared().photoUri{
let url = URL(string: imageUrl)
profileImageView.layer.masksToBounds = false
profileImageView.clipsToBounds = true
profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
profileImageView.contentMode = .scaleAspectFill
profileImageView.kf.setImage(with: url)
}
}
func openGallery(){
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
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 {
print("success") }
})
case .restricted:
print("User do not have access to photo album.")
case .denied:
print("User has denied the permission.")
}
}
}
extension MyProfileVC : UIImagePickerControllerDelegate,
UINavigationControllerDelegate{
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
self.profileImageView.image = image
print(info)
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
问题的原因是什么。
打印信息的时候调试区是这样的
[__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerReferenceURL): assets-library://asset/asset.JPG?id=54FE2CEF-59E7-4BCC-AF84-D64C1F32E58B&ext=JPG, __C.UIImagePickerControllerInfoKey( _rawValue: UIImagePickerControllerCropRect): NSRect: {{0, 0}, {2732.0000581881218, 2047.1787102689659}}, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerEditedImage): 大小 {748, 560} 方向 0 比例 1.000000(_rawValue=] : UIImagePickerControllerMediaType): public.image, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerOriginalImage): size {2732, 2048} orientation 0 scale 1.000000, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerPHAsset): 54FE2CEF-59E7-4BCC-AF84-D64C1F32E58B/L0/001 mediaType=1/4, sourceType=1, (2732x2048), creationDate=2018-09-17 02:01:23 +0000, location=0, hidden=0, favorite=0 , __C.UIImagePickerControllerInfoKey (_rawValue: UIImagePickerControllerImageURL): file:///private/var/mobile/Containers/Data/Application/CD64C336-B5F1-48F6-9D54-504006ADD6E2/tmp/DD94F454-F3BE-4EF9-9F6F-0C90F61BEE19.jpeg]
是我缺乏编程思维。
虽然我从照片库中选择了一张照片,但在 Activity 选择照片关闭后,viewWillAppear 会在 ImageView 上设置上一张图像。
我正在使用 swift4 开发一个 ios 应用程序。 点击profile image view时,在imageview上设置profile image,我有点击手势调用功能。
下面是我的代码。 我按照 swift 指南使用扩展实现了选择器委托。
看到几个关于同一个问题的文档,我想我做了我必须做的。但我无法找出错误,并在 imageview 上设置图像。 请检查我的代码。
import UIKit
import Kingfisher
import Alamofire
import Photos
class MyProfileVC: BaseNavigationVC {
@IBOutlet weak var stateMessageLabel: UILabel!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var nicknameLabel: UILabel!
@IBOutlet weak var sexImageView: UIImageView!
@IBOutlet weak var hash1Button: UIButton!
@IBOutlet weak var hash2Button: UIButton!
@IBOutlet weak var hash3Button: UIButton!
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tappedProfileImageView))
profileImageView.addGestureRecognizer(tap)
profileImageView.isUserInteractionEnabled = true
}
@objc func tappedProfileImageView(){
openGallery()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(false)
self.checkPermission()
//I updated this part. this code works correctly
if let imageUrl = UserInfo.shared().photoUri{
let url = URL(string: imageUrl)
profileImageView.layer.masksToBounds = false
profileImageView.clipsToBounds = true
profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
profileImageView.contentMode = .scaleAspectFill
profileImageView.kf.setImage(with: url)
}
}
func openGallery(){
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
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 {
print("success") }
})
case .restricted:
print("User do not have access to photo album.")
case .denied:
print("User has denied the permission.")
}
}
}
extension MyProfileVC : UIImagePickerControllerDelegate,
UINavigationControllerDelegate{
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
self.profileImageView.image = image
print(info)
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
问题的原因是什么。
打印信息的时候调试区是这样的
[__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerReferenceURL): assets-library://asset/asset.JPG?id=54FE2CEF-59E7-4BCC-AF84-D64C1F32E58B&ext=JPG, __C.UIImagePickerControllerInfoKey( _rawValue: UIImagePickerControllerCropRect): NSRect: {{0, 0}, {2732.0000581881218, 2047.1787102689659}}, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerEditedImage): 大小 {748, 560} 方向 0 比例 1.000000(_rawValue=] : UIImagePickerControllerMediaType): public.image, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerOriginalImage): size {2732, 2048} orientation 0 scale 1.000000, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerPHAsset): 54FE2CEF-59E7-4BCC-AF84-D64C1F32E58B/L0/001 mediaType=1/4, sourceType=1, (2732x2048), creationDate=2018-09-17 02:01:23 +0000, location=0, hidden=0, favorite=0 , __C.UIImagePickerControllerInfoKey (_rawValue: UIImagePickerControllerImageURL): file:///private/var/mobile/Containers/Data/Application/CD64C336-B5F1-48F6-9D54-504006ADD6E2/tmp/DD94F454-F3BE-4EF9-9F6F-0C90F61BEE19.jpeg]
是我缺乏编程思维。 虽然我从照片库中选择了一张照片,但在 Activity 选择照片关闭后,viewWillAppear 会在 ImageView 上设置上一张图像。