Reputation: 107
I'm making dating app. as you know, users need to register multiple pictures in dating app.
so i got how to use 1 image picker in one view.
but i don't know how to add multiple image picker.
i know i can only use only one
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
}
and
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(false, completion:nil)
}
so i cant find solution for multiple imagepicker view.
my failed code is below.
import UIKit
class RegisterPicture : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBAction func pick1(sender: AnyObject) {
let picker1 = UIImagePickerController()
picker1.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker1.allowsEditing = true
picker1.delegate = self
self.presentViewController(picker1, animated: false, completion: nil)
}
@IBAction func pick2(sender: AnyObject) {
let picker2 = UIImagePickerController()
picker2.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker2.allowsEditing = true
picker2.delegate = self
self.presentViewController(picker2, animated: false, completion: nil)
}
@IBOutlet var picture1: UIImageView!
@IBOutlet var picture2: UIImageView!
func imagePickerController(picker1: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker1.dismissViewControllerAnimated(false, completion : nil)
self.picture1.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerController(picker2: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker2.dismissViewControllerAnimated(false, completion : nil)
self.picture2.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker1: UIImagePickerController) {
picker1.dismissViewControllerAnimated(false, completion:nil)
}
func imagePickerControllerDidCancel(picker2: UIImagePickerController) {
picker2.dismissViewControllerAnimated(false, completion:nil)
}
}
Upvotes: 2
Views: 22573
Reputation: 719
For anyone searching for this in 2020, Apple introduced PHPickerViewController
in iOS14. Here's the documentation: https://developer.apple.com/documentation/photokit/phpickerviewcontroller
And the WWDC introduction that explains its usage: https://developer.apple.com/videos/play/wwdc2020/10652/
Upvotes: 11
Reputation: 398
You can't use UIImagePickerController, but you can use a custom image picker. I think ELCImagePickerController is the best option, but here are some other libraries you could use:
Objective-C
Swift
Upvotes: -1
Reputation: 11
@IBAction func btnTrophy1Action(_ sender: UIButton) {
//GIVE TAG TO YOUR BUTTON
self.view.endEditing(true)
self.senderTag = sender.tag
pickerOpen(sender: sender)
}
@IBAction func btnTrophy2Action(_ sender: UIButton) {
//GIVE TAG TO YOUR BUTTON
self.view.endEditing(true)
self.senderTag = sender.tag
pickerOpen(sender: sender)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
//BASED ON THE TAG SAVE IMAGE TO PARTICULAR IMAGE PICKER
if self.senderTag == 101 {
//TROPHY 1
self.imgTrophy1.image = image
self.isTrophy1Selected = true
} else if self.senderTag == 102 {
//TROPHY 2
self.imgTrophy2.image = image
self.isTrophy2Selected = true
} else {
//TROPHY 3
self.imgTrophy3.image = image
self.isTrophy3Selected = true
}
}
picker.dismiss(animated: true, completion: nil)
}
Upvotes: 1
Reputation: 836
I used a single image picker then use an enum holding strings to determine which image view gets the image selected by the user.
enum selectableImage: String {
case image1
case image2
case image3
}
then on each call of the imagePickerController
, I assign a variable var imageSelected = selectableImage.image1
like so.
Then finally in the imagePickerController
, I used the switched structure like so:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
print("Inside imageController")
print(info)
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let imagePicked = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
switch imageSelected {
case selectableImage.image1:
image1.image = imagePicked
case selectableImage.image2:
image2.image = imagePicked
case selectableImage.image3:
image3.image = imagePicked
}
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
Hope this helps.
Upvotes: 0