//I need help in how to pass data from a UIImagePickerController to a UIImageView , this is apart from a larger program i am developing
import Foundation
import UIKit
import MessageUI
class ImageSelect: UIViewController, UINavigationControllerDelegate , UIImagePickerControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func selectedImage(){ //func to choose a Picture from the library
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
let imgSelected = UIImagePickerController()
imgSelected.delegate = self
imgSelected.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imgSelected.allowsEditing = true
self.present(imgSelected, animated: true,completion: nil)
//Here its my problem*********************************
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
imgSelected1.image = UIImage(imgSelected)// give me a Error <
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//the imgSelected is a UIImagePickerController
//the imgSelected1 is a UIImageView
// how can i get the data from the imgSelected and pass it to the
imgSelected1??
//***************************************
}
}
}
how can I fix this , the error come with : Value of type 'UIImagePickerController' has no member 'image' . I know they are no the same type but I did not knew what else to put
You can't get image as you need to use imagePickerController's delegate method for pick selected image.
This method called automatically while you select image
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imgSelected1.image = selectedImage
}
Related
I'm very new to Xcode and Swift, and I was trying to display an image in a UIImageView that was selected using UIImagePickerController. I linked the UIImageView using an #IBOutlet and I put some if statements at the bottom to allow for editing of the selected picture, but I got stuck when an error popped up in my code. I'm pretty sure that the problem has something to do with how I am referencing the UIImageView, but I don't know how to do it in a different way. I just got this account to ask this question, so forgive me for the way I made this post.
Here is the code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func openPhotosButton(_ sender: UIButton) {
showImagePickerController()
}
#IBOutlet weak var characterView: UIImageView!
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func showImagePickerController() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
imagePickerController.sourceType = .photoLibrary
present(imagePickerController, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as UIImage {
characterView.image = editedImage
}
else if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as UIImage {
characterView.image = originalImage
}
dismiss(animated: true)
}
}
Picture from Xcode:
change your method to this one
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
characterView.image = editedImage
}
else if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
characterView.image = originalImage
}
dismiss(animated: true)
}
I am trying to build a simple app with a single button. Click on the button to take a picture, then using "TesseractOCR" I am converting the written text in the image to a string text and view it in my "Text View".
I got everything done, the camera and "TesseractOCR", the only problem I am facing is the following:
tesseract.image = UIImage(named: selectedImage)
Gives me this error:
Cannot convert value of type 'UIImage' to expected argument type 'String'.
Note: selectedImage suppose to be the name of the image that Tesseract takes to convert the image into text.
Here is my code:
import UIKit
import TesseractOCR
class secondViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, G8TesseractDelegate {
#IBOutlet weak var viewText: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
if let tesseract = G8Tesseract(language: "eng") {
tesseract.delegate = self
tesseract.image = UIImage(named: selectedImage)
tesseract.recognize()
textView.text = tesseract.recognizedText
}
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
}
Replace
tesseract.image = UIImage(named: selectedImage)
with
tesseract.image = selectedImage
the UIImage(named:<#string#>) takes a string value repressing the name of the image in bundle , but here you don't need it , instead supply the image directly
I am trying to select image from camera or gallery and showing the selected image to the screen in my image view. But it's not showing.
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
imageview.image = selectedImage
dismiss(animated: true, completion: nil)
}
What am I missing?
In my case the breakpoint function is not at all invoking
If your breakpoint function is not at invoking then I think you have not set the imagePicker delegate in viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
I faced same issue before and resolved it by setting image with delay like showed below:
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.imageview.image = selectedImage
})
Hope this will help.
Make sure you have UIImagePickerControllerDelegate added to your class and have set your UIImagePickerController instance delegate:
class PickerViewController: UIViewController, UIImagePickerControllerDelegate {
var pickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
pickerController.delegate = self
}
}
Also, it's better to dismiss the picker parameter sent to this delegate:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
...
picker.dismiss(animated: true, completion: nil)
}
I added UIView (A Class) as a xib instance to ViewController.
I want to get image from UIImagePickerController of ViewController class by action (in A Class) and return it to A Class.
I wrote up the following codes.
However, I do not know how to return to A Class.
I made delegate to ViewController again but I abandoned it because I displayed various errors of delegate. [Un wrap error ..., just did not move anything ... etc].
// A Class
class A: CustomDelegate{
var delegate001: PhotoLibraryDelegate!
class func instance() -> A {
return UINib(nibName: "A", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! A
}
#IBAction func openPhotoLibrary(_ sender: Any) {
self.delegate001.selectPhoto() //I want photo in A Class XD. so make Delegate!!
}
}
#objc protocol PhotoLibraryDelegate {
func selectPhoto()
}
//ViewController Class
class ViewController: PhotoLibraryDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let a = A.instance()
override func viewDidLoad() {
super.viewDidLoad()
a.delegate006 = self
}
func selectPhoto() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let pickerView = UIImagePickerController()
pickerView.sourceType = .photoLibrary
pickerView.delegate = self
self.present(pickerView, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage // Yeah, I selected the image!!
self.dismiss(animated: true)
//... Wa!? I want to return "image" to A class. OMG.
}
}
The simplest way is that you have already created the instance of A class. Now just directly assign the image to it.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage // Yeah, I selected the image!!
a.imageView.image = image
self.dismiss(animated: true)
}
I am creating a simple camera app.
In the code, UIImagePickerControllerclass is the delegate of ViewControllerClass instance.
But why "picker.delegate = self"is necessary in this code?
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBAction func launchCamera(_ sender: UIBarButtonItem) {
let camera = UIImagePickerControllerSourceType.camera
if UIImagePickerController.isSourceTypeAvailable(camera) {
let picker = UIImagePickerController()
picker.sourceType = camera
picker.delegate = self
self.present(picker, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageView.image = image
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
self.dismiss(animated: true)
}
delegate of UIImagePickerController includes UIImagePickerControllerDelegate and UINavigationControllerDelegate.
UIImagePickerControllerDelegate for catch users' action about take photo or choose picture.
UINavigationControllerDelegate for view controller navigation between album and photo