Image selected from iOS PhotoLibrary doesn't show in UIImageView - ios

I'm trying to use a UIImagePickerController to show a image picked from the Photo Library, but when I select the image, nothing happens.
All I'm doing is from Apple's Swift tutorial, so the code shouldn't be wrong, I guess. It doesn't work neither in simulator nor on a real device. I've tried all suggestions I found while googling. My code:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.delegate = self
}
//MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .photoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismiss(animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
}
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Text"
}
//MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
mealNameLabel.text = textField.text
}
}
I can provide more information if necessary. Thanks in advance.

Your delegate method is in the button action scope.
You have to write UIImagePickerControllerDelegate method out of the selectImageFromPhotoLibrary action scope and write with the proper signature.
Just like that:
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .photoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}

Related

how to select multiple images and videos in image from camera roll and display them in a collectionView?

I have been using following code to select an image and display it via UIImageView:
class ViewController UIViewContoller, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var imageView: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad(){
super.viewDidLoad()
imagePicker.delegate = self
}
#IBAction func loadImage(_ sender: Any){
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]){
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
imageView.contentMode = .scaleAspectFit
imageView.image = pickedImage
}
dismiss(animated: true completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
dismiss(animated: true, completion: nil)
}
}
How can I use UIImagePickerController class to select multiple images and videos and display them in a collection view?
How can I use UIImagePickerController class to select multiple images and videos
You can't. UIImagePickerController lets the user choose one image / video.

UIImagePickerController change bottom bar button title during image edit mode in Swift 3

I want to change the UIImagePickerController bottom bar buttons during
image editing mode in swift 3, Xcode 8.2.1.
Currently buttons are showing like as i mentioned in picture.
class MyController:UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var imageV: RoundedImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker.delegate = self
}
//MARK:- Action
#IBAction func libraryButtonPressed(_ sender: Any) {
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .photoLibrary
self.present(self.imagePicker, animated: true, completion: nil)
}
#IBAction func cameraButtonPressed(_ sender: Any) {
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .camera
self.present(self.imagePicker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.imageV.contentMode = .scaleAspectFit
self.imageV.image = pickedImage
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
Please suggest me..
Make sure you are not using Localization in your application, if you are using it, then properly configure all of your string files.
Have a look at this solution:
https://stackoverflow.com/a/42008789/2898708
I added PhotoLibrary.strings and in it I added following strings
"CANCEL" = "Cancel";
/* Title of button to set contact photo */
"SET_PHOTO_BUTTON" = "Set Photo";
"CHOOSE_PHOTO_BUTTON" = "Choose";
"USE_PHOTO_BUTTON" = "Use Photo";

Selecting photo returns to blank UI

I am trying to make an image uploader on my ios app.
EDIT 7
This question is now more clearly asked here: Present Modal for Image Select Erases UI
END OF EDIT 7
The first thing I am trying to do is enable the user to select an image that they want for upload.
EDIT 6
I have simplified all the code to simply one button press. The Modal presentation deletes the previous page's UI. How I prevent this.
import UIKit
class AddImageToStandViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func loadImageButtonTapped(sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
I have found that the selection works just fine and does not destroy the UI if I push/segue from a different to this controller.
END OF EDIT 6
Below this is the other methods that I have tried before this
So far, this code enables a user to press a button and choose a photo from their photos. But, after they click the photo it just returns them to an empty View except the navbar at the bottom.
It seems my user is being returned, not to the view that they came from, but to a separate empty view. How do I return the user to the view that they came from?
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var image: UIImageView!
...
#IBAction func selectPicture(sender: AnyObject) {
let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(ImagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
...
let me know if there is any more information that I should give.
EDIT 1
this code has the same effect
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
picker.dismissViewControllerAnimated(true, completion: nil)
}
I have tried adding this line below but I get an error stating that CreateStandViewController.Type cannot be converted to the expected UIViewController.
self.presentViewController(CreateStandViewController, animated: true, completion: nil)
EDIT 2
This code below doesnt error but also changes nothing
let createStandController = CreateStandViewController()
self.presentViewController(createStandController, animated: true, completion: nil)
EDIT 3
I have literally copied all the code from here: http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/ and now have:
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var imageView: UIImageView!
let imagePicker = UIImagePickerController()
#IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
This has the same issue as before. When the image is selected I am sent back to a view that is white with the navbar at the bottom.
EDIT 4
I have run the same code in its own separate project as a stand alone view controller and it works. There is some code that I have in the background of my app that is killing the functionality of the code (maybe). I am guessing it may have something to do with the navbar?
EDIT 5
I removed the tab bar at the bottom by seguing to the image selecting view via a present Modally segue, but it had no effect.
Just replace your
self.dismissViewControllerAnimated(true, completion: nil)
with
picker.dismissViewControllerAnimated(true, completion: nil)
I think you will get the point after that

iOS Camera with multiple imageView outputs

In my FirstVC I understand how to work with the UIImagePickerController, load the camera, take a photo, display it inside 1 imageView (still within the FirstVC) and then pass that image to an imageView inside the SecondVC via prepareForSegue to be shown over there.
What I need help with is having multiple imageViews inside FirstVC so that once a user takes the first picture it loads into the 1st imageView, then second picture the 2nd imageView, 3 pic to 3rd imageView, and 4th pic to 4th imageView. Afterwards an alert would appear that would say the user can't take any more pics and the user would press a nextSceneButton to be taken be taken to the SecondVC which would display the 4 chosen images that they just took.
I always used UIImagePickerController but I just got acquainted with AVFoundation. Should I use AVFoundation or UIImagePicker for this task?
Here is my UIImagePickerController code with only one imageView inside the FirstVC:
import UIKit
class FirstViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var nextSceneButton: UIButton!
//nextSceneButton is connected in StoryBoards and triggers the segue to the SecondVC
let imagePicker = UIImagePickerController()
var image: UIImage?
//MARK:- Custom Function
func noCamera(){
let alertVC = UIAlertController(title: "No Camera", message: "Sorry, this device has no camera", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style:.Default, handler: nil)
alertVC.addAction(okAction)
presentViewController(alertVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK:- Buttons
#IBAction func libraryButtonPressed(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func photoButtonPressed(sender: UIButton) {
if (UIImagePickerController.availableCaptureModesForCameraDevice(UIImagePickerControllerCameraDevice.Rear) != nil){
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Rear
presentViewController(imagePicker, animated: true, completion: nil)
}else{
noCamera()
}
}
//MARK:- ImagePickerController Delegates
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
self.image = chosenImage
imageView.image = self.image
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(true, completion: nil)
}
//MARK:- Segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == “fromPhotoVcToSecondVC” {
let secondVC = segue.destinationViewController as! SecondViewController
secondVC.image = self.image
}
}
}//end class
You can add a count property and hold your images in an array :
var count = 0
var imageViews:[UIImageView]
Every time you take a photo and delegate gets called load the respective imageview :
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageViews[count].image = chosenImage
count +=1
dismissViewControllerAnimated(true, completion: nil)
}
Then if all photos are taken alert the user :
#IBAction func photoButtonPressed(sender: UIButton) {
if count < imageViews.count {
//present UiimagePicker
} else {
// alert no more pictures allowed
}
}

Importing Camera Roll Image to UIView background

A small warning: I'm a complete noob at swift
Im creating a small app that (will eventually) allow a user to draw over an image they choose from their camera roll on their iPhone. However, im having some trouble getting the image to be set as the background to the UIView (drawView). Could someone give me some pointers as to how I could go about this? The code I have scraped together is shown below.
My ViewController File:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var drawView : AnyObject!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
#IBAction func clearTapped() {
var theDrawView: DrawView = drawView as! DrawView
theDrawView.lines = []
theDrawView.setNeedsDisplay()
}
let imagePicker = UIImagePickerController()
#IBAction func importImage(sender: AnyObject) {
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
drawView.backgroundImage = pickedImage
}
func dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
}
Also, my drawView has a separate swift file that deals with how the lines are drawn in it by the user.
you're going to want to separate your code a little more. right now, it seems like you're trying to create a controller within a controller. implement a class that is a subclass of UIIMagePickerController with the necessary delegate (you already have the code, so just copy and paste):
class MyImagePicker: UIImagePickerController, UIImagePickerControllerDelegate {
}
then in your view controller, initialize your image picker:
let myImagePicker = MyImagePicker()
call the correct method to return the image, set the background image of your UIView to that image, and then add that view as a subview:
drawView.backgroundImage = pickedImage
self.addSubview(pickedImage)
hope that works!

Resources