Why "picker.delegate = self necessary"? - ios

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

Related

Image not showing up in ImageView after using Image Picker

I am trying to write a simple app that will update the ImageView with a photo from my photos library. I can open the photos library and select a photo, but after I do that the default image in the ImageViewer does not show up. Any idea why?
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBOutlet weak var photoView: UIImageView!
#IBAction func testGesture(_ sender: UITapGestureRecognizer) {
let imagePickerController = UIImagePickerController()
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: [String : Any]) {
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage.rawValue] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
photoView.image = selectedImage
dismiss(animated: true, completion: nil)
}
}
You're not using the UIImagePickerControllerDelegate's didFinishPickingMediaWithInfo method. Remove the private and modify the didFinishPickingMediaWithInfo method to the UIImagePickerControllerDelegate method syntax and you're good to go.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
photoView.image = selectedImage
print(selectedImage)
dismiss(animated: true, completion: nil)
}

How do you display an image in a UIImageView that was selected using UIImagePickerController?

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

UIImage is not showing after selecting image from gallery or camera

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

How to use UIImagePickerController with delegate?

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

Why doesn't UIImagePickerController delegate to my Object class?

class CameraPicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var viewController:MyProfileVC!
func launchCamera() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker:UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
imagePicker.cameraCaptureMode = .photo
imagePicker.allowsEditing = false
self.viewController.present(imagePicker, animated: true, completion: nil)
} }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("didFinishPickingMedia")
}
This is my object class function, but 'didFinishPickingMediaWithInfo' function doesn't get called after taking the picture. Also, the viewcontroller which is presenting the imagepicker is a different Swift file
I had the same problem and I've found a solution, so I'm posting my version (I'm taking a picture from the photo library but it's the same :) ).
I had a memory management issue.
I've created an IBAction function where I instantiated my camera handler class (with the delegate inside...). At the end of the function the variable goes out of scope and it's deallocated. To solve the issue I've made it as instance variable.
That's my code for the VC with my UiButton:
class STECreateUserVC: UIViewController {
#IBOutlet weak var imgAvatar: UIImageView!
let cameraHandler = STECameraHandler()
#IBAction func buttonPressed(_ sender: UIButton) {
cameraHandler.importPictureIn(self) { [weak self] (image) in
self?.imgAvatar.image = image
}
}
}
...and that's my handler:
class STECameraHandler: NSObject {
let imagePickerController = UIImagePickerController()
var completitionClosure: ((UIImage) -> Void)?
func importPictureIn(_ viewController: UIViewController, completitionHandler:((UIImage) -> Void)?) {
completitionClosure = completitionHandler
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
imagePickerController.sourceType = .photoLibrary
imagePickerController.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
viewController.present(imagePickerController, animated: true, completion: nil)
}
}
extension STECameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let completitionClosure = completitionClosure, let image = info[UIImagePickerControllerEditedImage] as? UIImage {
completitionClosure(image)
}
imagePickerController.dismiss(animated: true)
}
}
I've used a closure in order to have a cleaner code.
This:
self.viewController.present(imagePicker, animated: true, completion: nil)
should be:
self.present(imagePicker, animated: true, completion: nil)

Resources