IBOutlet is not being recognized as an UIImageView - swift

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

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

UIImageView is not showing image selected via UIIMagePickerController

I am trying to select an image using the UIImagePickerController . I have the following code in which there is one IBOutlet of UIImageView.
After selecting the image in didFinishPickingMediaWithInfo I get the image in memory but somehow it is not visible in the imageview. I did try to change a static image in didFinishPickingMediaWithInfo for imageview but it didn't show the image either. The code is as below,
class SnapshotViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate
{
#IBOutlet var imageView: UIImageView!
#IBAction func takePhoto(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = false
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
dismissViewControllerAnimated(true, completion: nil)
dispatch_async(dispatch_get_main_queue())
{
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.imageView.image = image
}
}
}
I guess this is general problem that whatever image i am changing for this imageView in didFinishPickingMediaWithInfo is not working. If i change the imageView image in viewDidLoad then it works.
Can anyone please help as i need to show the image selected via photo library?
It is strongly recommended not to update UI controls from a background thread. This can be the cause of strange behaviour and crashes which are sometimes very hard to identify.
Try to set the image on the main thread instead:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.imageView.image = image
dismissViewControllerAnimated(true, completion: nil)
}
I think this code would solve your problem and make the picture from your photolibrary appear:
class PostViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet var imageToPost: UIImageView!
#IBAction func chooseAnImage(_ sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageToPost.image = image
}
self.dismiss(animated: true, completion: nil)
}
In swift 4, I tried various things but non of them work until I just add #objc annotation to didFinishPickingMediaWithInfo override method:
Pressenting UIImagePickerController:
#objc func handleSelectProfileImageView () {
let pickerController = UIImagePickerController()
pickerController.delegate = self
present(pickerController, animated: true, completion: nil)
}
Extension to get media data (in this one I had to add #objc):
extension MyViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImage = image
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}
About #objc from doc:
Apply this attribute to any declaration that can be represented in
Objective-C—for example, nonnested classes, protocols, nongeneric
enumerations (constrained to integer raw-value types), properties and
methods (including getters and setters) of classes, protocols and
optional members of a protocol, initializers, and subscripts. The objc
attribute tells the compiler that a declaration is available to use in
Objective-C code.

how to show same picked image in two Image Views in Xcode swift 3?

I am unable to figure out how to use picked image and show it in two image view at the same time. Basically, I want user to click on button then select an image and after that the image is shown in two different image views( Using Swift 3 and Xcode 8 ).
#IBOutlet var imageview: UIImageView!
var imagePicker = UIImagePickerController()
#IBOutlet weak var effectView: UIVisualEffectView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageview.image = UIImage(named:"Selfie_4072_5194.jpg")
imagePicker.delegate = self
imageview2.image = imageview.image
imagePicker.delegate = self
}
#IBAction func camerabutton(_ sender: UIButton) {
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
#IBAction func libraryBtn(_ sender: AnyObject) {
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imageview.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: {})
}
#IBAction func Screenshot(_ sender: UIButton) {
I can't see where you are defining imageview2 make sure you have added it as an outlet, and linked it in your storyboard.
#IBOutlet var imageview2: UIImageView!
It also looks like you are only setting the image for imageview try setting the image property of your imageview2 too.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageview.image = image
imageview2.image = image
}
self.dismiss(animated: true, completion: {})
}