There are 5 image selectors in the same VC, how to pass the image selector to the relative image,
Here is my current code
#IBAction func setPicture1(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
}
#IBAction func setPicture2(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
}
.....
This is an image upload, how do I transfer the image to the relative image?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
image1.image = image
}
}
Related
I am having an issue while displaying camera captured image in an image view where delegates are set and being called "didFinishPickingMediaWithInfo".
I have two buttons for the user interaction (camera picture & from gallery), once I take picture from camera and then select any other image from gallery; the camera picture taken earlier is shown for a second. I tried searching a lot but no luck so far.
Can anyone please advise what am I missing. Following is the code for reference.
#IBAction func cameraButtonPressed(_ sender: UIButton) {
self.openCamera()
}
#IBAction func galleryButtonPressed(_ sender: UIButton) {
self.openGallary()
}
override func viewDidLoad() {
super.viewDidLoad()
initialLayout()
}
override func viewWillAppear(_ animated: Bool) {
self.lblPreview.isHidden = true
self.imageTake.isHidden = true
self.uploadButtonOutlet.isHidden = true
}
//
//MARK: - Internal Methods
func initialLayout() {
self.cameraButtonOutlet.layer.cornerRadius = 20
self.galleryButtonOutlet.layer.cornerRadius = 20
self.uploadButtonOutlet.layer.cornerRadius = 20
}
//MARK: - Open the camera
func openCamera(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)){
imagePicker.sourceType = UIImagePickerController.SourceType.camera
//If you dont want to edit the photo then you can set allowsEditing to false
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.cameraCaptureMode = .photo
imagePicker.cameraDevice = .rear
self.present(imagePicker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//MARK: - Choose image from camera roll
func openGallary(){
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
imagePicker.allowsEditing = true
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
extension UploadTimesheetViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
self.lblPreview.isHidden = false
self.imageTake.isHidden = false
self.uploadButtonOutlet.isHidden = false
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.imageTake.image = editedImage
self.imageTake.setNeedsLayout()
})
// self.imageTake.image = editedImage
}
//Dismiss the UIImagePicker after selection
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.isNavigationBarHidden = false
self.dismiss(animated: true, completion: nil)
}
}
Update your delegate method like this,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var finalImage:UIImage?
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
finalImage = image
}else {
finalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
}
self.lblPreview.isHidden = false
self.imageTake.isHidden = false
self.uploadButtonOutlet.isHidden = false
self.imageTake.image = finalImage
//Dismiss the UIImagePicker after selection
picker.dismiss(animated: true, completion: nil)
}
Sorry for the delayed response. Just updating the answer so if anyone is having the same issue should know what was the actual problem.
Once picker is dismissed a viewwillappear will be called where I have hid the image view and hence the reason it was not displaying it for the first time.
If I remove the code from viewwillappear and add that properties code to viewedload then all works fine.
The issue was once the picker is dismissed , viewillapear was called and that was creating the issue.
when I am trying to implement all the uiimagepickerview methods in my app, my view controller (after choosing an image) shows nothing on it? It should have my textfields however they all 'disppeared'
Here's my code
#IBAction func registerUserProfileImageSelectButton(_ sender: UIButton) {
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 {
registerUserProfileImage.contentMode = .scaleAspectFit
registerUserProfileImage.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
I suspect it may be something to do in my dismiss method, but I am not too certain
Thanks for your help in advance!
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
The problem is you are trying to dismiss the current view controller, not the image picker controller. Please use:
imagePicker.dismiss(animated: true, completion: nil)
That should work!
this works for me perfectly you can try this code :
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBOutlet weak var imagePickerImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func imagePicker(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self // you need to set a delegate to your image picker here
//to get selected ( edited ) portion
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print(info)
// let img = info[UIImagePickerControllerOriginalImage] as! UIImage
// if you keep editing true
let img = info[UIImagePickerControllerEditedImage] as! UIImage
imagePickerImg.image = img
dismiss(animated: true, completion: nil)
}
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
print("Cancel Press")
dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am trying to select the images each with 1 button but it not displaying any images which I had selected.
var imagePicker = UIImagePickerController()
#IBAction func chooseImage1(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Button capture")
imagePicker.delegate = self
imagePicker.sourceType = .SavedPhotosAlbum
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
#IBAction func chooseImage2(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Button capture")
imagePicker2.delegate = self
imagePicker2.sourceType = .SavedPhotosAlbum
imagePicker2.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
chooseImage1.image = pickedImage
let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage
chooseImage2.image = pickedImage2
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
I would like to be able to select two individual different photos on the imageView each. Thank you for the help.
UIImagePickerController can only pick images one at a time. To do a multiple selection, you should use a well updated API like this.
I've looked at a lot of solutions which, sadly, did not work for me. I used this code to create an imagePickerController:
#IBAction func takePhoto(sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, _didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
dismissViewControllerAnimated(true, completion: nil)
}
But still the image I take on the camera doesn't show up in the imageView. I also imported the correct delegates. When I try to print the imageView.image to the console it returns nil. What am I doing wrong?
Please try this code. Hope it will help you.
func cameraPhoto(){
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.cameraCaptureMode = .Photo
imagePicker.cameraDevice = .Front;
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
}
Thanks
I am using UIImagePickerController to pick images from both Camera and Library. When i get the image from the library the picture is OK. However, when i get the picture from the camera its rotating 90 degrees to the right. My app works just in portrait mode. Here is the code where i get the image
#IBAction func camPhoto(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func importPhoto(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
my Function importPhoto works fine but the other one doesn't.
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedPhoto = info[UIImagePickerControllerOriginalImage] as! UIImage
img.image = selectedPhoto
dismissViewControllerAnimated(true, completion: {
self.convBtn.hidden = false
})
}
Any ideas?
You can try like :
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedPhoto = info[UIImagePickerControllerOriginalImage] as! UIImage
**selectedPhoto.withHorizontallyFlippedOrientation()**
img.image = selectedPhoto
dismissViewControllerAnimated(true, completion: {
self.convBtn.hidden = false
})
}