How to Make Uiimagepickercontroller for Camera and Photo Library at the Same Time in Swift

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

Accessing the camera and photo library in swift 4

Here is the code to load image from photos & camera in iOS.

⁃ You need to create an outlet of your UIImageView

⁃ Then add a tap gesture on to image view

⁃ Then connect tap gesture with the didTapOnImageView function.

⁃ Then add the following extension to your view controller.

//MARK:- Image Picker
extension YourViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

//This is the tap gesture added on my UIImageView.
@IBAction func didTapOnImageView(sender: UITapGestureRecognizer) {
//call Alert function
self.showAlert()
}

//Show alert to selected the media source type.
private func showAlert() {

let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
self.getImage(fromSourceType: .camera)
}))
alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
self.getImage(fromSourceType: .photoLibrary)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
self.present(alert, animated: true, completion: nil)
}

//get image from source type
private func getImage(fromSourceType sourceType: UIImagePickerController.SourceType) {

//Check is source type available
if UIImagePickerController.isSourceTypeAvailable(sourceType) {

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

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

self.dismiss(animated: true) { [weak self] in

guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
//Setting image to your image view
self?.profileImgView.image = image
}
}

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

}

Note: Don't forget to add the privacy settings in info.plist

Privacy - Camera Usage Description

Privacy - Photo Library Usage Description

iOS 11 - always opens photo library even the source type change it from .photoLibrary to .camera

func startCameraFromViewController(_ viewController: UIViewController, withDelegate delegate:
UIImagePickerControllerDelegate & UINavigationControllerDelegate) -> Void {

if (UIImagePickerController.isSourceTypeAvailable(.camera) == false) {
print("fail")
}

let cameraController = UIImagePickerController()
cameraController.sourceType = .camera
cameraController.allowsEditing = true
cameraController.delegate = delegate

present(cameraController, animated: true, completion: nil)

}

This is the code that works for me. Same problem on iOS 11, but working with this. Maybe you need to remove self.present(imgPicker, animated: true, completion: nil) in buttonProfilePicPressed method.

Camera and photo library together in iOS

you tried the instagram SDK? Has a method that works for me

How to convert the image to base64 after take a photo from camera or library

you could use something like this approach, using the .onDismiss()
to then convert your imageSelected to base64 string using your ImageConverter.

In ProfileView,

 .sheet(isPresented: $openCameraRoll, onDismiss: didDismiss) {
ImagePicker(selectedImage: $imageSelected, sourceType: .camera)
}

func didDismiss() {
let b64Str = imageManager.imageToBase64(imageSelected)
print("\n---> b64Str: \(b64Str?.count) \n")
}

Also remove the ZStack, or replace it by a VStack

EDIT-1: here some example code that works for me:

struct ProfileView: View {
let imageManager = ImageConverter()

@State var changeProfileImage = false
@State var openCameraRoll = false
@State var imageSelected = UIImage()

var body: some View {
VStack {
Button(action: {
openCameraRoll = true
}, label: {
Image(systemName: "plus")
// .profileImageMod()
})
if changeProfileImage {
Image(uiImage: imageSelected)
.resizable() // <-- here
.frame(width: 222, height: 222)
.foregroundColor(.white)
.background(Color.gray)
.clipShape(Circle())
} else {
Image(systemName: "questionmark")
.frame(width: 222, height: 222)
.foregroundColor(.white)
.background(Color.gray)
.clipShape(Circle())
}
}
.sheet(isPresented: $openCameraRoll, onDismiss: didDismiss) {
ImagePicker(selectedImage: $imageSelected, sourceType: .camera)
}
}

func didDismiss() {
changeProfileImage = true
let b64Str = imageManager.imageToBase64(imageSelected)
print("\n---> b64Str: \(b64Str?.count) \n")
}
}


Related Topics



Leave a reply



Submit