I am writing my first iOS application (iPhone only) with Swift. The main application view should allow user to choose the image from the photo gallery.
I've found the following sample code of ViewController.swift:
class ViewController: UIImagePickerController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { var imagePickerController = UIImagePickerController() imagePickerController.delegate = self imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum imagePickerController.allowsEditing = true self.presentViewController(imagePickerController, animated: true, completion: { imageP in }) } func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) { let selectedImage : UIImage = image println(selectedImage) } }
and have the following View Controller Scene -
View Controller - Top Layout Guide - Bottom Layout Guide - View - Image View First Responder Exit
But when I start the app, just black screen is shown. What I am doing wrong? Another sample code I've found is in Objective-C, which doesn't help me.
To load and display an image in iOS app we'll first need to get an image. Then we'll drag that image to our project and select copy if required option and our application target.
To access the camera in Swift, we first must ask the user for permission. Open up your Info. plist file from your Project Navigator. Then right click and click Add Row .
How to Display an Image Picker in Swift 1 Prerequisites. Have XCode installed on your machine. ... 2 Getting started. Here, we're going to start from scratch. ... 3 Setting up view controller. The first thing we want to do is to connect our Storyboard elements into our view controller class. ... 4 Displaying image picker. ...
If your Swift iOS app needs to import a picture from the user’s device, you’ve come to the right place today. Let’s learn how to use UIImagePickerController to let the user select a photo from their device to load into your app. This is going to be a very simple storyboard. It’s going to be a button, and a UIImageView (to show our loaded image).
The source type is the options that give the user to pick a picture from the library or saved photos album or pick a freshly taken photo. Here’s our function ⬇️ Next, we’ll create a function that will display an image picker controller based on the source type chosen by the user (Library or Camera)
The image can be obtained from different types of sources, such as the user's photo library or their phone's camera. Before you get started, make sure that you: Have XCode installed on your machine. Have a basic knowledge of Swift programming language. Here, we're going to start from scratch.
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 } }
Complete copy-paste working image picker for swift 4 based on @user3182143 answer:
import Foundation import UIKit class ImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { var picker = UIImagePickerController(); var alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet) var viewController: UIViewController? var pickImageCallback : ((UIImage) -> ())?; override init(){ super.init() let cameraAction = UIAlertAction(title: "Camera", style: .default){ UIAlertAction in self.openCamera() } let galleryAction = UIAlertAction(title: "Gallery", style: .default){ UIAlertAction in self.openGallery() } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel){ UIAlertAction in } // Add the actions picker.delegate = self alert.addAction(cameraAction) alert.addAction(galleryAction) alert.addAction(cancelAction) } func pickImage(_ viewController: UIViewController, _ callback: @escaping ((UIImage) -> ())) { pickImageCallback = callback; self.viewController = viewController; alert.popoverPresentationController?.sourceView = self.viewController!.view viewController.present(alert, animated: true, completion: nil) } func openCamera(){ alert.dismiss(animated: true, completion: nil) if(UIImagePickerController .isSourceTypeAvailable(.camera)){ picker.sourceType = .camera self.viewController!.present(picker, animated: true, completion: nil) } else { let alertController: UIAlertController = { let controller = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert) let action = UIAlertAction(title: "OK", style: .default) controller.addAction(action) return controller }() viewController?.present(alertController, animated: true) } } func openGallery(){ alert.dismiss(animated: true, completion: nil) picker.sourceType = .photoLibrary self.viewController!.present(picker, animated: true, completion: nil) } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.dismiss(animated: true, completion: nil) } //for swift below 4.2 //func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { // picker.dismiss(animated: true, completion: nil) // let image = info[UIImagePickerControllerOriginalImage] as! UIImage // pickImageCallback?(image) //} // For Swift 4.2+ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { picker.dismiss(animated: true, completion: nil) guard let image = info[.originalImage] as? UIImage else { fatalError("Expected a dictionary containing an image, but was provided the following: \(info)") } pickImageCallback?(image) } @objc func imagePickerController(_ picker: UIImagePickerController, pickedImage: UIImage?) { } }
Call it from your viewcontroller like this:
ImagePickerManager().pickImage(self){ image in //here is the image }
Also don't forget to include the following keys in your info.plist
:
<key>NSCameraUsageDescription</key> <string>This app requires access to the camera.</string> <key>NSPhotoLibraryUsageDescription</key> <string>This app requires access to the photo library.</string>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With