in Education by
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. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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: NSCameraUsageDescription This app requires access to the camera. NSPhotoLibraryUsageDescription This app requires access to the photo library.

Related questions

0 votes
    I am using Realm with Swift 3 in my iOS app. I have the following code //Find all records for the day func ... { let predicate = NSPredicate(format: "date >= %@ and date...
asked May 8, 2022 in Education by JackTerrance
0 votes
    Is it possible to get access to dismiss() in an SKScene class? Dismiss is a method available from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    Can anyone help me, Im trying to make a simple app where you touch the screen and 4 images are ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Can anyone help me, Im trying to make a simple app where you touch the screen and 4 images are ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I make an alarm clock app, and I want when my device is locked and alarm start calling see a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    Recently implemented trailingSwipeActionsConfigurationForRowAt , where after swiping from right to left showing two options ... , JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I've inherited a codebase that implements a half-baked implementation of custom intents and shortcuts. One ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I'm making a little game and I'am having trouble updating an UILabel I created to display the current ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I am writing an API for an iOS app where the user's login with the Facebook API on iOS. The server ... achieve this kinda setup? Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
0 votes
    Would you recommend that users go through the guest user request process to allow them to share a file from SharePoint with an external person?...
asked Mar 10, 2021 in Technology by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
...