swift - How do I take a photo programmatically in iOS using takePicture()? -
i working on app assist visually impaired people read restaurant menus, need photo captured via single double-tap of screen.
i've used uiimagepickercontroller take photos using standard capture interface. in order via double-tap, have set overlay view general imageview, disabled camera controls , called .takepicture() method. reason, delegate method handles capturing of image doesn't work. can please point me missing?
i have commented line on present image picker if called can never leave it. overlay has no buttons photo taking supposed carried out double-tap.
any assistance appreciated.
best regards,
andre.
code below:
import uikit class viewcontroller: uiviewcontroller, uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate { @iboutlet weak var debuglabel: uilabel! @iboutlet var tapchangestate: uitapgesturerecognizer! @iboutlet weak var imageview: uiimageview! var imagepicker = uiimagepickercontroller() // instantiates image picker controller. override func viewdidload() { super.viewdidload() // setting image picker imagepicker.delegate = self // setup view controller image picker delegate. imagepicker.allowsediting = false // disable image editing user imagepicker.sourcetype = uiimagepickercontrollersourcetype.camera // set source camera imagepicker.cameradevice = uiimagepickercontrollercameradevice.rear // use rear camera // setting gesture recognizer tapchangestate.numberoftouchesrequired = 1 // number of fingers required change state detection tapchangestate.numberoftapsrequired = 2 debuglabel.text = "loaded" } @ibaction func tapchangestatedetected(_ sender: uitapgesturerecognizer) { imagepicker.cameraoverlayview = imageview imagepicker.showscameracontrols = false // disable camera controls imagepicker.takepicture() // takes shot debuglabel.text = "tap detected." //present(imagepicker, animated: true, completion: nil) } // conforming controllerdelegate protocol: func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : any]) { if let selectedimage = info[uiimagepickercontrolleroriginalimage] as? uiimage { imageview.contentmode = .scaleaspectfill imageview.image = selectedimage debuglabel.text = "captured" } dismiss(animated: true, completion: nil) } func imagepickercontrollerdidcancel(_ picker: uiimagepickercontroller) { dismiss(animated: false, completion: nil) } // controller delegate protocol functions end here }
1. uiimagepickercontroller
needs presented 1st before taking picture.
present(imagepicker, animated: false) { self.imagepicker.takepicture() }
2. makes sure "camera usage permission" set in info.plist.
<key>privacy - camera usage description</key> <string>my awesome app requires access phone’s camera.</string>
3. cameraoverlayview
view overlayed on default camera ui. believe in example don't want use imageview
because view getting set captured image. if set cameraoverlayview
imageview
imageview
is removed main view controller hierarchy. example working, removed imagepicker.cameraoverlayview = imageview
ibaction.
Comments
Post a Comment