Multiple Image Pickers in One Controller

Multiple Image Pickers in one Controller

The delegate function imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) also returns the UIImagePickerController that finished picking an image.

So you can use an if statement to check if the picker that finished is pickerOne or pickerTwo. Then you implement different behaviour according to the result of that.

Maybe set the pickers to nil after they have finished to clean up some memory.

class multiPickerVC : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var pickerOne : UIImagePickerController?
var pickerTwo : UIImagePickerController?

override func viewDidLoad() {
//
}

@IBAction func getBackground(sender: AnyObject) {
pickerTwo = UIImagePickerController()
pickerTwo!.delegate = self
pickerTwo!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerTwo!.allowsEditing = true
self.presentViewController(pickerTwo!, animated: true, completion: nil)
}

@IBAction func selectAvatar(sender: AnyObject) {
pickerOne = UIImagePickerController()
pickerOne!.delegate = self
pickerOne!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerOne!.allowsEditing = true
self.presentViewController(pickerOne!, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

if picker == pickerOne {

// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.addAvatar.setImage(image, forState: .Normal)

} else if picker == pickerTwo {

// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage

self.headerImage.image = image
}

self.dismissViewControllerAnimated(true, completion: nil)

}
}

Multiple Image uploads in One View Controller with different Buttons (iOS, Xcode9, Swift4)

Change your button IBaction code to set image picked based on button click so that your code work like charm

@IBAction func uploadImage1(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 1
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}

@IBAction func uploadImage2(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 2
let imagePicker1 = UIImagePickerController()
imagePicker1.delegate = self
imagePicker1.sourceType = .photoLibrary;
imagePicker1.allowsEditing = true
self.present(imagePicker1, animated: true, completion: nil)
}
}
@IBAction func uploadImage3(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 3
let imagePicker2 = UIImagePickerController()
imagePicker2.delegate = self
imagePicker2.sourceType = .photoLibrary;
imagePicker2.allowsEditing = true
self.present(imagePicker2, animated: true, completion: nil)
}
}
@IBAction func uploadImage4(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 4
let imagePicker3 = UIImagePickerController()
imagePicker3.delegate = self
imagePicker3.sourceType = .photoLibrary;
imagePicker3.allowsEditing = true
self.present(imagePicker3, animated: true, completion: nil)
}
}

Want to add multiple images by extracting one method

The easiest way to handle this is of course passing "which avatar" info down to _imgFromCamera or _imgFromGallery .
For example, if your 3 images have their own identities like 'A', 'B' and 'C',

onTap: () {
_showPicker(context, 'A');
},

Then pass it to the _showPicker.

void _showPicker(context, id) {
...
onTap: () {
_imgFromGallery(id); // Passed 'A' in this example
Navigator.of(context).pop();
}),
...

Then,

_imgFromGallery(id) async {
final image = await Picker.pickImage(
source: ImageSource.gallery, imageQuality: 50
);

// Depends on the id, decide where and how to save the image.
switch(id) {
case 'A': setState(...); break;
case 'B': saveIntoLocal(...); break;
case 'C': uploadToServer(...); break;
....
}
}

How to display two images on two UIImageViews one View Controller, UIImagePickerController's?

What you observe is that when the user taps on any of the image views (wallpaperPhoto or profilePhoto), the UIImagePickerController always uses self as its delegate. Then, when the user picks an image, the delegate cannot distinguish any more which image view originally was tapped.

You could simply add a weak optional variable indicating the "active" tapped image view, and then set only it's image. Also, you can reduce the tap handler to a single function with a sender argument, which is the image view the user tapped on, and set the activeImageViewto this.

extension SettingProfileViewController:UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var activeImageView:UIImageView? = nil

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
activeImageView.image = image
}
dismiss(animated: true, completion: nil)
}

override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelect(_:))
profilePhoto.addGestureRecognizer(tapGesture)
profilePhoto.isUserInteractionEnabled = true

let wallTapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelect(_:)))
wallpaperPhoto.addGestureRecognizer(wallTapGesture)
wallpaperPhoto.isUserInteractionEnabled = true
}

@objc func handleSelect(sender:UIGestureRecognizer) {
guard let sendingImageView = sender.view as? UIImageView else {
print("Ooops, received this gesture not from an ImageView")
return
}
activeImageView = sendingImageView
let pickerController = UIImagePickerController() //открывает галерею
pickerController.delegate = self
present(pickerController, animated: true, completion: nil)
}

// ...

one UIImagePickerController for multiple UIImageViews in a single VC

You can make an enum to store the possible picker types (profile or ID picture).

enum PictureSelectionType {
case profilePicture
case idPicture
}
class DriverAplication: UIViewController {
var pictureSelectionType = PictureSelectionType.profilePicture

/// inside ViewDidLoad...
let profileImgTapGestur = UITapGestureRecognizer(target: self, action: #selector(DriverAplication.selectProfileImage))
driverPImg.addGestureRecognizer(profileImgTapGestur)

let passImgTapGestur = UITapGestureRecognizer(target: self, action: #selector(DriverAplication.selectIDImage))
passImg.addGestureRecognizer(passImgTapGestur)


@objc func selectProfileImage() {
/// remember what the picker should return
pictureSelectionType = .profilePicture

let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()

let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.allowsEditing = true
present(pickerController, animated: true, completion: nil)

}
@objc func selectIDImage() {
/// remember what the picker should return
pictureSelectionType = .idPicture

let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()

let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.allowsEditing = true
present(pickerController, animated: true, completion: nil)
}
}

extension DriverAplication: UINavigationControllerDelegate, UIImagePickerControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

var returnedImage: UIImage?
if let editedImg = info[.editedImage] as? UIImage {
returnedImage = editedImg
} else if let originalImg = info[.originalImage] as? UIImage {
returnedImage = originalImg
}

if let image = returnedImage { /// unwrap the image

/// read what the selection type is
if pictureSelectionType == .profilePicture {
driverPImg.image = image
} else { /// for ID picture
passImg.image = image
}
}

dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}

Upload multiple images with image-picker plugin and send to controller with ajax in asp.net core?

Here is a demo to pass multiple files to action with ajax:

View:

<div class="row">
<div id="coba1"></div>
</div>
<button onclick="uploadfiles()">upload</button>
<script>
$("#coba1").spartanMultiImagePicker({
fieldName: 'fileUpload[]'
});
function uploadfiles() {
var Form_Data = new FormData();
document.getElementsByName("fileUpload[]").forEach((el) => {
if ($(el)[0].files[0] != undefined)
Form_Data.append('ImagesGallery', $(el)[0].files[0]);
}

);
$.ajax({
url: 'AddAndUpdateProduct',
data: Form_Data,
contentType: false,
processData: false,
type: 'POST',
success: function (data) {
//
}
});
}
</script>

action:

public IActionResult AddAndUpdateProduct(AddProductViewModel model)
{
return Ok();
}

result:
Sample Image



Related Topics



Leave a reply



Submit