Swift - Uiimagepickercontroller - How to Use It

Swift - UIImagePickerController - how to use it?

You're grabbing a UIImage named UIImagePickerControllerOriginalImage and there exists no such image. You're meant to grab the UIImage with the key UIImagePickerControllerOriginalImage from the editingInfo dictionary:

let tempImage = editingInfo[UIImagePickerControllerOriginalImage] as! UIImage

How to make UIImagePickerController for camera and photo library at the same time in swift

Import UIImagePickerControllerDelegate and create a variable to assign UIImagePickerController
var imagePicker = UIImagePickerController() and set imagePicker.delegate = self.

Create an action sheet to display options for 'Camera' and 'Photo library'.

On your button click action:

@IBAction func buttonOnClick(_ sender: UIButton)
{
self.btnEdit.setTitleColor(UIColor.white, for: .normal)
self.btnEdit.isUserInteractionEnabled = true

let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
self.openCamera()
}))

alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
self.openGallary()
}))

alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

/*If you want work actionsheet on ipad
then you have to use popoverPresentationController to present the actionsheet,
otherwise app will crash on iPad */
switch UIDevice.current.userInterfaceIdiom {
case .pad:
alert.popoverPresentationController?.sourceView = sender
alert.popoverPresentationController?.sourceRect = sender.bounds
alert.popoverPresentationController?.permittedArrowDirections = .up
default:
break
}

self.present(alert, animated: true, completion: nil)
}

func openCamera()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera))
{
imagePicker.sourceType = UIImagePickerController.SourceType.camera
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
else
{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}

func openGallary()
{
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}

Download sample project for Swift, SwiftUI

How to use UIImagePickerController in swift 4

You need to dismiss end of the function didFinishPickingMediaWithInfo

picker.dismiss(animated:true,completion:nil)

Swift 5 UIImagePickerController is not getting called

You seem to be missing setting delegate for the UIImagePickerController. Replace your onTapImageView with this:

@objc func onTapImageView(){
DispatchQueue.main.async {
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .photoLibrary
self.imagePicker.delegate = self
self.present(self.imagePicker, animated: true, completion: nil)
}
}

Also you need the controller to confirm to UINavigationControllerDelegate so add conformance to FirstViewController like this:

class FirstViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

one UIImagePickerController for multiple UIImageViews in a single VC

You can make an enum to store the possible picker types (profile or ID picture).

enum PictureSelectionType {
case profilePicture
case idPicture
}
class DriverAplication: UIViewController {
var pictureSelectionType = PictureSelectionType.profilePicture

/// inside ViewDidLoad...
let profileImgTapGestur = UITapGestureRecognizer(target: self, action: #selector(DriverAplication.selectProfileImage))
driverPImg.addGestureRecognizer(profileImgTapGestur)

let passImgTapGestur = UITapGestureRecognizer(target: self, action: #selector(DriverAplication.selectIDImage))
passImg.addGestureRecognizer(passImgTapGestur)


@objc func selectProfileImage() {
/// remember what the picker should return
pictureSelectionType = .profilePicture

let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()

let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.allowsEditing = true
present(pickerController, animated: true, completion: nil)

}
@objc func selectIDImage() {
/// remember what the picker should return
pictureSelectionType = .idPicture

let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()

let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.allowsEditing = true
present(pickerController, animated: true, completion: nil)
}
}

extension DriverAplication: UINavigationControllerDelegate, UIImagePickerControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

var returnedImage: UIImage?
if let editedImg = info[.editedImage] as? UIImage {
returnedImage = editedImg
} else if let originalImg = info[.originalImage] as? UIImage {
returnedImage = originalImg
}

if let image = returnedImage { /// unwrap the image

/// read what the selection type is
if pictureSelectionType == .profilePicture {
driverPImg.image = image
} else { /// for ID picture
passImg.image = image
}
}

dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}

How to use UIImagePickerController with delegate?

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)

}

How to allow user to pick the image with Swift?

If you just want let the user choose image with UIImagePickerController use this code:

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet var imageView: UIImageView!
@IBOutlet var chooseBuuton: UIButton!
var imagePicker = UIImagePickerController()

@IBAction func btnClicked() {

if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
print("Button capture")

imagePicker.delegate = self
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = false

present(imagePicker, animated: true, completion: nil)
}
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in

})

imageView.image = image
}
}


Related Topics



Leave a reply



Submit