i have created an app menu where users can click on a photo and the pic is displayed in an alertView and there i have added 2 actions -> One of the action being cancel and other action being edit images which should open gallery to select some other image. however when i click on the edit button(which has code added) It does nothing and work same as to dismiss the alert view. the code for the ALERTVIEW is
@objc func taxImageTApped(_snder:UITapGestureRecognizer) {
print("TaxImage")
let alertView = UIAlertController(title: "Edit Tax Image", message: "", preferredStyle: UIAlertController.Style.alert)
let image = #imageLiteral(resourceName: "[email protected]")
let uiImageAlertAction = UIAlertAction(title: "", style: .default, handler: nil)
let scaleSze = CGSize(width: 245, height: 245/image.size.width*image.size.height)
let reSizedImage = image//.resize(newSize: scaleSze)
uiImageAlertAction.setValue(reSizedImage.withRenderingMode(.alwaysOriginal), forKey: "image")
alertView.addAction(uiImageAlertAction)
alertView.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: nil))
alertView.addAction(UIAlertAction(title: "Edit", style: .default) {action in
let newImage = UIImagePickerController()
newImage.delegate = self
newImage.sourceType = UIImagePickerController.SourceType.photoLibrary
newImage.allowsEditing = false
})
self.present(alertView, animated: true, completion: nil)
}
/////////////////////below is details for tap gesture i have applied on //the label
taxImageView.translatesAutoresizingMaskIntoConstraints = false
taxImageView.textColor = tableTextColor
taxImageView.text = "View Image"
taxImageView.textAlignment = .left
taxImageView.attributedText = NSAttributedString(string: "View Image", attributes:
[.underlineStyle: NSUnderlineStyle.single.rawValue])
editInfoView.addSubview(taxImageView)
taxImageView.leftAnchor.constraint(equalTo: editInfoView.leftAnchor, constant: 260).isActive = true
taxImageView.topAnchor.constraint(equalTo: editInfoView.topAnchor, constant: 740).isActive = true
taxImageView.widthAnchor.constraint(equalToConstant: 300).isActive = true
taxImageView.heightAnchor.constraint(equalToConstant: 20).isActive = true
taxImageView.isUserInteractionEnabled = true
let taxImageGesture = UITapGestureRecognizer.init(target: self, action: #selector(taxImageTApped))
taxImageGesture.numberOfTapsRequired = 1
taxImageGesture.isEnabled = true
taxImageGesture.cancelsTouchesInView = false
taxImageView.gestureRecognizerShouldBegin(taxImageGesture)
taxImageView.addGestureRecognizer(taxImageGesture)
Try this one
Create variable in controller
let imagePickerController = UIImagePickerController()
Add this is in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
imagePickerController.modalPresentationStyle = .popover
}
Call this function from where you want or you can add as gesture method also
func addActionSheet() {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let galleryOption = UIAlertAction(title: "Choose Photo", style: .default, handler: { action in
self.imagePickerController.sourceType = .photoLibrary
self.present(self.imagePickerController, animated: true, completion: nil)
})
let cameraOption = UIAlertAction(title: "Take Photo", style: .default, handler: { action in
self.imagePickerController.sourceType = .camera
self.present(self.imagePickerController, animated: true, completion: nil)
})
let deleteOption = UIAlertAction(title: "Delete Photo", style: .default, handler: { action in
self.imageView.image = nil
})
let cancelOption = UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
self.dismiss(animated: true, completion: nil)
})
alertController.addAction(galleryOption)
alertController.addAction(cameraOption)
alertController.addAction(deleteOption)
alertController.addAction(cancelOption)
self.present(alertController, animated: true, completion: nil)
}
Add Delegate method of UIImagePickerControllerDelegate
or UINavigationControllerDelegate
to get image
extension ControllerName: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageView.image = image
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}