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 UIImagePickerControllervar 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
Setting Up a Plist to Store Application Data (Not Settings) for an iPhone Game
Sending Latitude and Longitude to Server When App Is in Background
Swift Dynamic Table Cell Height
Itms-90809: Deprecated API Usage - Existing App That Use Uiwebview Are No Longer Accepted
Flutter: Disable Swipe to Navigate Back in iOS and Android
How to Set the Rootviewcontroller with Swift, iOS 7
How to Get 18-Digit Current Timestamp in Swift
Custom Font on Uibutton Title Clipped on Top of Word
Error Domain=Nsposixerrordomain Code=100 "Protocol Error"
Does iPhone Support Hardware-Accelerated Aes Encryption
Get Device Current Orientation (App Extension)
Checking Push Notification Registration: Isregisteredforremotenotifications Not Updating
Submitting iOS App Using Beta Version of Xcode
Open iOS 11 Files App via Url Scheme or Some Other Way
Swift 3 Nsnotificationcenter Keyboardwillshow/Hide
Swift: Change Status Bar Color for iOS 13
iOS - Linking Framework Storyboard to Viewcontroller for Use in Main Project