I have two images in screen so i am using chooser:ImageChooser here chooser will indicates which button i have clicked
with this code if i taken picture from camera then its preview in profilePicImage.image and rightProfilePicImg.image not showing but if i pass this value in JSON parameter the image value is passing
code: here if i choose gallery image then chosen image preview is showing in both images but if i take picture from camera its preview is not showing in both images why? please guide me
enum ImageChooser {
case profile,right
}
let imagePickerEdit = UIImagePickerController()
var chooser:ImageChooser = .profile
@IBAction func profilePicBtn(_ sender: UIButton) {
chooser = .profile
}
@IBAction func rightProfileBtn(_ sender: UIButton) {
chooser = .right
let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
self.openCamera()
}))
alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
self.openGallery()
}))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
@IBAction func profilePicBtn(_ sender: UIButton) {
chooser = .profile
let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
self.openCamera()
}))
alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
self.openGallery()
}))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func openCamera()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
imagePickerEdit.delegate = self
imagePickerEdit.sourceType = UIImagePickerController.SourceType.camera
imagePickerEdit.allowsEditing = false
self.present(imagePickerEdit, 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)
}
}
func openGallery()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
// let imagePicker = UIImagePickerController()
imagePickerEdit.delegate = self
imagePickerEdit.allowsEditing = true
imagePickerEdit.sourceType = UIImagePickerController.SourceType.photoLibrary
self.present(imagePickerEdit, animated: true, completion: nil)
}
else
{
let alert = UIAlertController(title: "Warning", message: "You don't have permission to access gallery.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
extension NewEditProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[.originalImage] as? UIImage {
if chooser == .profile {
profilePicImage.image = pickedImage
}
if chooser == .right {
rightProfilePicImg.image = pickedImage
}
}
picker.dismiss(animated: true, completion: nil)
}
}
o/p: here image 1 picture taken from camera but its preview not showing in profilePicImage.image .. image 2 picture chosen from gallery so its preview showing in rightProfilePicImg.image
