Uiimagepickercontroller in Swift 3

UIImagePickerController in Swift 3

I think you need to add privacy - key for camara and photo library in your info.plist file as from xcode 8. for example,

 Key : Privacy - Media Library Usage Description
Value : YES

Key : Privacy - Photo Library Usage Description
Value : YES

Key : Privacy - Camara Usage Description
Value : YES

here value is string not Boolean.

so try this.

Check Apple documentation for more details!

Reference : this so post

Swift 3 select multiple photos using UIImagePickerController

As Ethan Halprin commented, I used https://github.com/hyperoslo/ImagePicker and it solved my issue. It looks really nice and I think the customer will like it :)

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

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



Related Topics



Leave a reply



Submit