Use of unresolved identifier with an image view - swift

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

Related

IBOutlet is not being recognized as an UIImageView

I am trying to allow users to take photos when the button action is clicked. I've added an UIImageView into storyboard which is to my View Controller. I've set the name for this control as imageView, yet when I implement imageView.image = image I am getting an error "Cannot find 'imageView' in scope". I've also tried to set this as self.imageView = image and I get the error "Cannot find self in scope". I am unsure why my imageView which is connected to the #IBOutlet is not being found?
import UIKit
class HomeScreenViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet weak var imageView: UIImageView!
var button: UIButton! {
imageView.backgroundColor = .secondarySystemBackground
self.button.backgroundColor = .systemBlue
}
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func didTapButton(){
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.delegate = self
present(picker, animated: true)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
return
}
imageView.image = image
}
Your function is not inside your class!! You should put the function inside the class, like this:
class HomeScreenViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]){
// any code
}
}

Cannot infer contextual base in reference to member 'originalImage'

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.

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

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.