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)
}
Related
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 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
}
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
I have a class
class ActivitiesMainPageController: UICollectionViewController ,UIImagePickerControllerDelegate, UINavigationControllerDelegate{
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch indexPath.item {
case 0 :
let takePictureInstance = takePictures()
takePictureInstance.presentCamera()
}
}
The takePictures() is s custom class
class takePictures: UIViewController, UIImagePickerControllerDelegate, UIAlertViewDelegate, UINavigationControllerDelegate
{
var imagePicker: UIImagePickerController! = UIImagePickerController()
func presentCamera()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
{
let objViewController = UIApplication.shared.keyWindow?.rootViewController
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
objViewController?.present(imagePicker,animated: true,completion: nil)
}
else
{
// error msg
print ("error displaying Camera")
noCamera()
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
print(self.Tag + "image picker controller")
self.dismiss(animated: true, completion: nil)
}
}
The didfinishpickingimage never gets triggered. I don't see the print statement. After reading the SO here, I think I am not doing the delegate correctly. Can you please help?
The problem is in your ActivitiesMainPageController class. It's a memory management issue. You create a local instance of your takePictures class (FYI - class names should start with uppercase letters), and then call the presentCamera method. And then the takePictureInstance variable immediately goes out of scope. This means it has no more references and it gets deallocated.
So at this point, the image picker's (weak) delegate becomes nil. This is why the image picker delegate method is never called.
There are two ways to fix this:
Keep a strong reference to the takePictures instance inside your ActivitiesMainPageController class (use a property instead of a local variable). But this has the problem of not knowing when to release the instance.
Perform a little trick inside the takePictures class where it keeps a strong reference to itself when created, and then releases that strong reference after the image picker is dismissed.
Option 2 can be implemented like this:
class takePictures: UIViewController, UIImagePickerControllerDelegate, UIAlertViewDelegate, UINavigationControllerDelegate
{
var strongSelf: takePictures?
var imagePicker: UIImagePickerController! = UIImagePickerController()
func presentCamera()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
{
let objViewController = UIApplication.shared.keyWindow?.rootViewController
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
objViewController?.present(imagePicker,animated: true,completion: nil)
strongSelf = self // keep me around
}
else
{
// error msg
print ("error displaying Camera")
noCamera()
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
print(self.Tag + "image picker controller")
self.dismiss(animated: true, completion: nil)
strongSelf = nil // let me go
}
}
Why are you presenting the UIImagePicker from the takePictures class (Should be TakePictures)? I think the problem is originating because the view controller you're currently in is not the one presenting the picker. If you pulled that presentCamera() function out and put it in the ActivitiesMainPageControllerand assigned <- that as the delegate and also switched the method signature to the correct one:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
You should have better luck.
I'm trying to implement this framework from Github:
https://github.com/hyperoslo/ImagePicker
Then I created an IBAction in my view controller as described in the Description:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate {
#IBAction func addMemory(sender: UIBarButtonItem) {
let imagePickerController = ImagePickerController()
imagePickerController.delegate = self //Error here
present(imagePickerController, animated: true, completion: nil)
}
}
Error: Cannot assign value of type 'MemoriesTable' to type
'ImagePickerDelegate?'
You should be implementing ImagePickerDelegate as well, because delegate is of type ImagePickerDelegate. Change as follow
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate, ImagePickerDelegate {
Make sure you implement protocol methods.
EDIT 1
You need to implement following methods of ImagePickerDelegate
public protocol ImagePickerDelegate: class {
func wrapperDidPress(imagePicker: ImagePickerController, images: [UIImage])
func doneButtonDidPress(imagePicker: ImagePickerController, images: [UIImage])
func cancelButtonDidPress(imagePicker: ImagePickerController)
}
Revisit my own question 10 months later.
Simply change this line:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate
to:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate
will make it work.
if you are implement the ImagePickerController then you need to add the following delegate UINavigationControllerDelegate,UIImagePickerControllerDelegate on your class
at the same time you need to implement the following delegate methods also
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
Ive had similar issue. I realized picker.delegate = self was missing. Adding it fixed the issue
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker:UIImage?
if let editedImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{
selectedImageFromPicker = editedImage
print(editedImage.size)
}
else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{
selectedImageFromPicker = originalImage
print(originalImage.size)
}
if let selectedImage = selectedImageFromPicker{
Profileimg.image = selectedImage
}
dismiss(animated: true, completion: nil)
}