How do you press the "use photo" button programmatically - ios

I am trying to make an app the takes pictures every so often with this code
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var cameraIsOn = false
let imagePicker = UIImagePickerController()
var timeInterval = 15
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = .camera
}
#IBAction func PlayButton(_ sender: UIBarButtonItem) {
present(imagePicker, animated: true) {
self.cameraIsOn = true
self.imagePicker.takePicture()
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imageView.image = image
}
imagePicker.dismiss(animated: true) {
self.present(self.imagePicker, animated: true) {
self.cameraIsOn = true
if self.cameraIsOn {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(self.timeInterval * 60), execute: {
self.imagePicker.takePicture()
})
}
}
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
cameraIsOn = false
}
}
After a picture is taken. You are shown a "retake or use photo screen".
I am trying to press the "use photo" button programmatically so that I can complete the loop until someone presses cancel.

Not sure of what button(use photo) action you are looking for! but to call/press a UIButton programmatically. Try this
yourButton.sendActions(for: .touchUpInside)
It would be better if you make your question more detailed. What is your scenario? What exactly you are tying to achieve?

Related

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

This question already has answers here:
Picking two different images in the same view controller using imagePickerController in Swift
(1 answer)
Select Multiple Images (UIImagePickerController or Photos.app Share UI)
(8 answers)
Closed 3 years ago.
I want to pick images from phone library when I tap on two different UIImageViews and after selection, display them on two different UIImageView's,
but I when run the following code, the same image displays at two different UIImageViews, How can I fix it?
'''
extension SettingProfileViewController:UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
profilePhoto.image = image
print("profile")
}
if let wallImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
wallpaperPhoto.image = wallImage
print("wallpaper")
}
dismiss(animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelectProfilePhotoView))
profilePhoto.addGestureRecognizer(tapGesture)
profilePhoto.isUserInteractionEnabled = true
let wallTapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelectWallpaperImageView))
wallpaperPhoto.addGestureRecognizer(wallTapGesture)
wallpaperPhoto.isUserInteractionEnabled = true
}
#objc func handleSelectProfilePhotoView(){
let pickerController = UIImagePickerController() //открывает галерею
pickerController.delegate = self
present(pickerController, animated: true, completion: nil)
}
#objc func handleSelectWallpaperImageView(){
let pickerCont = UIImagePickerController()
pickerCont.delegate = self
present(pickerCont, animated: true, completion: nil)
}
'''
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)
}
// ...

Checking which gestureRecognizer was pressed

I use gestureRecognizer with UIImageView. And I have two UIImageView. Me need upload image in both UIImageView. For opening UIImagePickerController() I use gestureRecognizer on both UIImageView.
So me need upload image in different UIImageView in func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
How I can upload image from library?
It's my code:
#IBOutlet weak var mainPhotoImageView: UIImageView!
#IBOutlet weak var addImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
configureGestureRecognizer()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
if chosenImage != nil {
logoImageView.image = chosenImage
dismiss(animated: true, completion: nil)
}
}
#objc func addLogoHall(_ sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
#objc func addMainPhoto(_ sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
func configureGestureRecognizer() {
let addHallLogo = UITapGestureRecognizer(target: self, action: #selector(AddPhotoAndLogoViewController.addLogoHall(_:)))
logoImageView.isUserInteractionEnabled = true
logoImageView.addGestureRecognizer(addHallLogo)
let addMainPhotoImage = UITapGestureRecognizer(target: self, action: #selector(AddPhotoAndLogoViewController.addMainPhoto(_:)))
mainPhotoImageView.isUserInteractionEnabled = true
mainPhotoImageView.addGestureRecognizer(addMainPhotoImage)
}
So in func imagePickerController me need detect which gestureRecognizer was used. Or maybe have alternative opportunity to do it?
You can add diffrent tag for pickers like this way:
#objc func addLogoHall(_ sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.view.tag = 1
present(picker, animated: true, completion: nil)
}
#objc func addMainPhoto(_ sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.view.tag = 2
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
And then you can identify by this tag like this way:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
if chosenImage != nil {
if(picker.view.tag == 1) {
logoImageView.image = chosenImage
} else {
mainImageView.image = chosenImage
}
dismiss(animated: true, completion: nil)
}
}
While Jogendar's answer is good, I see one pitfall. The if-else will grow as the number of image view instances increase in your view controller file. This might get messy and unnecessarily large quickly. Might I suggest an alternative solution:
class ImageSelectionController: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var delegate: ImageSelectionControllerDelegate?
private weak var imageView: UIImageView?
private weak var presentingViewController: UIViewController?
// MARK: - Initialization
init(withImageView imageView: UIImageView?, andPresentingViewController viewController: UIViewController?) {
self.imageView = imageView
self.presentingViewController = viewController
}
// MARK: - Public
func presentImagePickerController(withConfigurationHandler handler: ((UIImagePickerController) -> Void)? = nil) -> Void {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.sourceType = .photoLibrary
handler?(picker)
picker.delegate = self
}
// MARK: - UIImagePickerController
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
self.imageView?.image = chosenImage
self.delegate?.controller(self, didFinishPickingImage: chosenImage, forImageView: self.imageView)
picker.dismiss(animated: true, completion: nil)
}
}
protocol ImageSelectionControllerDelegate: AnyObject {
func controller(_ controller: ImageSelectionController, didFinishPickingImage image: UIImage?, forImageView: UIImageView?) -> Void
}
// ---- Your View Controller ---- //
#IBOutlet weak var logoImageView: UIImageView?
#IBOutlet weak var mainPhotoImageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
configureGestureRecognizer()
}
#objc func addLogoHall(_ sender: AnyObject) {
let controller = ImageSelectionController.init(withImageView: self.logoImageView, andPresentingViewController: self)
controller.delegate = self
controller.presentImagePickerController()
}
#objc func addMainPhoto(_ sender: AnyObject) {
let controller = ImageSelectionController.init(withImageView: self.logoImageView, andPresentingViewController: self)
controller.presentImagePickerController { (picker) in
picker.sourceType = .camera
}
}
// MARK: - ImageSelectionControllerDelegate
func controller(_ controller: ImageSelectionController, didFinishPickingImage image: UIImage?, forImageView: UIImageView?) {
print("image picked!")
}
Doing it this way, you could put all the UIImagePickerController related code in one place and you wouldn't need to have any if-else cases. Again, there is no right or wrong answer and someone might also find some other flaw in my solution. Its just a matter of how you want to structure your code.
Happy coding!

IOS Swift imagePickerController not call

class MyPage: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
#IBOutlet weak var selectImage: UIButton!
override func viewDidLoad() {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
}
#IBAction func selectImage(_ sender: Any) {
print("a")
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
print("b")
imagePicker.allowsEditing = false
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true,completion: nil)
}
#objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
print("dismiss")
})
print(image)
selectImage.setImage(image, for: .normal)
}
}
If touch button print a and b and pop photoLibrary
but select Photo not call imagePickerController
do nothing after select photo
how to use imagepicker in swift 4?
Remove #objc from Delegate method and delegate method is this so replace your function with this :
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
and get image by using this :
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {return}
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let imageView = UIImageView()
let imagepicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.imagepicker.delegate = self
imageView.frame = CGRect(x: 40, y: 150, width: 100, height: 100)
self.view.addSubview(imageView)
}
#IBAction func uploadImage(_ sender : UIButton) {
imagepicker.cameraDevice = .rear
imagepicker.cameraCaptureMode = .photo
imagepicker.sourceType = .photoLibrary
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
imageView.image = image
print(image)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}

This is about album and memory

I'm using Swift, and I called once the album, with a systematic approach, and then allowed to dismiss the normal way (perhaps even less the other way), but after a call to complete systems approach, memory will rise 20M, I wrote a simple example, found that still the case, I wrote immediate window in appdelegate. Swift rootViewController switch inside, still can not eliminate this 20M memory. Because I called up a photo album directly 20M, but many times I'll call the album now, not in this demo, so I need to solve this 20M problem.
The code:
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
var bu = UIButton()
bu.frame = CGRectMake(80, 80, 80, 80)
bu.backgroundColor = UIColor.redColor()
bu.addTarget(self, action: "hh", forControlEvents: .TouchUpInside)
self.view.addSubview(bu)
}
func hh() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
picker.dismissViewControllerAnimated(true, completion: nil)
var tt = TViewController()
self.presentViewController(tt, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController){
picker.dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

How to present UIImagePickerController from UIImageView with Tap Gesture

I want the view to load with a default image and let the user tap this image to select another from the photo library. I'm doing this way, but the tap gesture is not working, what am I doing wrong?
class ProfileViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let imagePicker = UIImagePickerController()
#IBOutlet var profilePicture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: Selector("selectProfilePicture"))
profilePicture.addGestureRecognizer(tap)
imagePicker.delegate = self
}
func selectProfilePicture() {
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
profilePicture.image = image
}
}
Try to enable userInteractionEnabled on your UIImageView:
profilePicture.userInteractionEnabled = true
I tend to just put an invisible UIButton on top of the image. Easiest / simplest way to do it, without overcomplicating it.

Resources