IOS Swift imagePickerController 未调用
IOS Swift imagePickerController not call
class MyPage: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
@IBOutlet weak var selectImage: UIButton!
override func viewDidLoad() {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
}
@IBAction func selectImage(_ sender: Any) {
print("a")
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
print("b")
imagePicker.allowsEditing = false
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true,completion: nil)
}
@objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
print("dismiss")
})
print(image)
selectImage.setImage(image, for: .normal)
}
}
如果触摸按钮打印 a 和 b 并弹出照片库
但是select拍照不来电imagePickerController
select 照片后什么都不做
如何在swift4中使用图像选择器?
从 Delegate
方法中删除 @objc
并且 delegate
方法是这样的,所以用这个替换你的函数:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
并使用此获取图像:
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {return}
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let imageView = UIImageView()
let imagepicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.imagepicker.delegate = self
imageView.frame = CGRect(x: 40, y: 150, width: 100, height: 100)
self.view.addSubview(imageView)
}
@IBAction func uploadImage(_ sender : UIButton) {
imagepicker.cameraDevice = .rear
imagepicker.cameraCaptureMode = .photo
imagepicker.sourceType = .photoLibrary
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
imageView.image = image
print(image)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
class MyPage: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
@IBOutlet weak var selectImage: UIButton!
override func viewDidLoad() {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
}
@IBAction func selectImage(_ sender: Any) {
print("a")
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
print("b")
imagePicker.allowsEditing = false
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true,completion: nil)
}
@objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
print("dismiss")
})
print(image)
selectImage.setImage(image, for: .normal)
}
}
如果触摸按钮打印 a 和 b 并弹出照片库
但是select拍照不来电imagePickerController
select 照片后什么都不做
如何在swift4中使用图像选择器?
从 Delegate
方法中删除 @objc
并且 delegate
方法是这样的,所以用这个替换你的函数:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
并使用此获取图像:
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {return}
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let imageView = UIImageView()
let imagepicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.imagepicker.delegate = self
imageView.frame = CGRect(x: 40, y: 150, width: 100, height: 100)
self.view.addSubview(imageView)
}
@IBAction func uploadImage(_ sender : UIButton) {
imagepicker.cameraDevice = .rear
imagepicker.cameraCaptureMode = .photo
imagepicker.sourceType = .photoLibrary
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
imageView.image = image
print(image)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}