Cannot infer contextual base in reference to member 'originalImage' - swift

im trying to use imagePickerController and im getting this weird error "Cannot infer contextual base in reference to member 'originalImage'"
I've used this code before but im not sure why im getting this error this time, or how to fix it.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
guard let pickImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
photoImageView.image = pickImage
dismiss(animated: true, completion: nil)
}
please help!
heres the full code:
class AddCardViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var saveButton: UIBarButtonItem!
#IBOutlet weak var questionField: UITextField!
#IBOutlet weak var answerField: UITextField!
#IBOutlet weak var photoImageView: UIImageView!
private let imagePickerController = UIImagePickerController()
let defaultImage = UIImage(named: "q")
private var newCard: Card?
private func updateSaveButtonState() {
let questionText = questionField.text ?? ""
let answerText = answerField.text ?? ""
saveButton.isEnabled = !questionText.isEmpty && !answerText.isEmpty
}
public func getCard() -> Card? {
return newCard
}
override func viewDidLoad() {
super.viewDidLoad()
photoImageView.image = defaultImage
questionField.delegate = self
answerField.delegate = self
}
#IBAction func pickImage(_ sender: Any) {
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let pickImage:UIImage = info [.originalImage] as? UIImage else {
fatalError("expected a dictionary containing an image, but was provided the following: \(info)")
}
photoImageView.image = pickImage
dismiss(animated: true, completion: nil)
}
func alertMessage(_ message: String) {
let alertController = UIAlertController(title: "Add a card", message: message, preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "okie dokie", style: UIAlertAction.Style.default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}

I just checked your code and it works for me.
It could be that you don't have an IBOutlet for photoImageView
If it isn't that, then I would need to see the rest of your imagePickerController code to understand what's wrong.

The issue was I was using Swift 4 when I should've been using Swift 5.

Related

How to add 2 or more images into email from photo gallery to MFMailComposeViewController [duplicate]

I am making an app in which there are two UIImageViews. In each image view the user needs to be able to input a different image. Here is the code I have so far.
var imagePicker = UIImagePickerController()
#IBAction func chooseImage1(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Button capture")
imagePicker.delegate = self
imagePicker.sourceType = .SavedPhotosAlbum
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
#IBAction func chooseImage2(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Button capture")
imagePicker2.delegate = self
imagePicker2.sourceType = .SavedPhotosAlbum
imagePicker2.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
chooseImage1.image = pickedImage
let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage
chooseImage2.image = pickedImage2
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
This ends up picking the same image for each different image view. I would like to be able to choose two individual photos, one for each view. Thank you for your help.
You only need one UIImagePickerController. You can keep a reference of the tapped view and when the user finish picking the image, you just need to cast the selected view as UIImageView and set its image property:
update: Xcode 11.5 • Swift 5.2 or later
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate ,UINavigationControllerDelegate {
#IBOutlet weak var imageView1: UIImageView!
#IBOutlet weak var imageView2: UIImageView!
var imagePicker = UIImagePickerController()
var selectedVew: UIView!
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = false
[imageView1,imageView2].forEach {
$0?.isUserInteractionEnabled = true
$0?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseImage)))
}
}
#objc func chooseImage(_ gesture: UITapGestureRecognizer) {
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
selectedVew = gesture.view
present(imagePicker, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
(selectedVew as? UIImageView)?.image = info[.originalImage] as? UIImage
dismiss(animated: true)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true)
}
}

Use UIImagePicker to set the image in a UIImageView

This is how my code currently looks like. What I expect to happen is the profileImage (UIImageView) to display the selected image as currently it is not happening.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var profileImage: UIImageView!
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.sourceType = .photoLibrary
}
#IBAction func photoUploadPressed(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
selectedImage = image
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}
Currently the Image Picker comes up, but once selected it does not change the UIImageView's image.
Any help will be much appreciated.
It is working as expected only error in your code is that you have } missing in photoUploadPressed(_ sender: Any) and you have not declared selectedImage & using it inside imagePickerController
Working code
import UIKit
class ViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let picker = UIImagePickerController()
var selectedImage: UIImage?
#IBOutlet weak var profileImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
picker.delegate = self
picker.sourceType = .photoLibrary
}
#IBAction func potoUploadPressed(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}// It is missing in your code
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
selectedImage = image // You have not declared selectedImage
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}

Use of unresolved identifier with an image view

Im trying to make a pick selecter but I get Use unresolved identifier 'imageusr' . I tried changing the Target Membership and with that I get a lot more errors. imageusr.image = image ( here its where it shows the error )
Import UIkit class ViewController2: UIViewController {
#IBOutlet weak var imageusr: UIImageView!
var imagepick = UIImagePickerController()
#IBAction func seleccionar(_ sender: Any) {
imagepick.sourceType = .photoLibrary
imagepick.allowsEditing = true
present(imagepick, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imageusr.roundedImage()
imagepick.delegate = (self as! UIImagePickerControllerDelegate & UINavigationControllerDelegate)
// Do any additional setup after loading the view.
} here
enter extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
imageusr.image = image
}
dismiss(animated: true, completion: nil)
}
}
Looks like it's a code alignment issue, the extension should be declared outside of the class
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imageusr: UIImageView!
var imagepick = UIImagePickerController()
#IBAction func seleccionar(_ sender: Any) {
imagepick.sourceType = .photoLibrary
imagepick.allowsEditing = true
present(imagepick, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imageusr.roundedImage()
imagepick.delegate = self
// Do any additional setup after loading the view.
}
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
imageusr.image = image
}
dismiss(animated: true, completion: nil)
}
}

I'm trying to display an image from the photo library in a UIImageView

The full error is:
2018-11-17 11:48:21.587818-0700 TestApp[3763:162426] [discovery]
errors encountered while discovering extensions: Error
Domain=PlugInKit Code=13 "query cancelled"
UserInfo={NSLocalizedDescription=query cancelled}
My code:
import UIKit
class CameraViewController: UIViewController {
#IBOutlet weak var captionTextView: UITextView!
#IBOutlet weak var photo: UIImageView!
#IBOutlet weak var shareButton: UIButton!
var selectedImage: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
let tapGesturn = UITapGestureRecognizer(target: self, action: #selector(self.handleSelectPhoto))
photo.addGestureRecognizer(tapGesturn)
photo.isUserInteractionEnabled = true
}
#objc func handleSelectPhoto() {
let pickerController = UIImagePickerController()
pickerController.delegate = self
present(pickerController, animated: true, completion: nil)
}
#IBAction func shareButton_TouchUpInside(_ sender: Any) {
}
}
extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("did Finish Picking Media")
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage{
selectedImage = image
photo.image = image
}
dismiss(animated: true, completion: nil)
}
}
Instead of using string "UIImagePickerControllerOriginalImage" as key for info dictionary use info key for original image UIImagePickerController.InfoKey.originalImage
So replace this
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
with this
if let image = info[.originalImage] as? UIImage {
also replace info dictionary type in parameters of delegate method from [String : Any] to [UIImagePickerController.InfoKey : Any]
imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
and last replace private keyword before delegate method with #objc

Choose photo from library in swift doesn't work

In my app I have a "addImgBtn" that should let the user to choose image from his photos. also I have "profileImg" that store some temporary image.
When I click the "addImg" button it shows me all photos, but when I click "Choose" it doesn't replace the temporary image with the chosen one, here my code:
class myClass: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet weak var profileImg: UIImageView!
#IBOutlet weak var addImgBtn: UIButton!
#IBAction func addImgBtn(_ sender: Any) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
})
profileImg.image = image
}
}
You are using the wrong imagePickerController method, change to the following:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
self.dismiss(animated: true, completion: { () -> Void in
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.profileImg.image = image
}
})
}
This should work! As a general tip, command-click into UIImagePickerControllerDelegate or any delegate you are implementing in xcode and it will tell you all available methods,etc.