ios - How to make a UIButton take a picture? -


i started working on ios. making custom camera show imageview of camera. want know how connect uibutton take picture.

ensure have "uinavigationcontrollerdelegate" , "uiimagepickercontrollerdelegate" in class e.g...

class viewcontroller: uiviewcontroller, uinavigationcontrollerdelegate, uiimagepickercontrollerdelegate { 

i work on idea have imageview linked storyboard, button.

then inside button function created ibaction add following code...

if uiimagepickercontroller.issourcetypeavailable(uiimagepickercontrollersourcetype.camera) {             let img = uiimagepickercontroller()             img.delegate = self             img.sourcetype = uiimagepickercontrollersourcetype.camera             img.allowsediting = false              self.presentviewcontroller(img, animated: true, completion: nil)         } else {  print("no camera available") } 

firstly, check if camera available.

next, want place picture taken imageview following code..

func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingimage image: uiimage, editinginfo: [string : anyobject]?) {         addimageview.image = image         self.dismissviewcontrolleranimated(true, completion: nil)     } 

note: "addimageview" name of imageview using.

it won't work on simulator, but will if connect device.

when run on device, allow take camera photo (snap picture) , place in relavant imageview selected.