ios - imagePickerController crashes when fetching photos from camera. From photo library works fine. Why? -
in swift
app i'm allowing user add photo - either camera or photo library. has choice:
@ibaction func captureimage(_ sender: anyobject) { let imagefromsource = uiimagepickercontroller() imagefromsource.delegate = self imagefromsource.allowsediting = false let alertcontroller = uialertcontroller( title: "what want do?", message: "choose action.", preferredstyle: .actionsheet) let selectpictureaction = uialertaction( title: "choose image gallery", style: .default) { (action) -> void in imagefromsource.sourcetype = uiimagepickercontrollersourcetype.photolibrary self.present(imagefromsource, animated: true, completion: nil) } alertcontroller.addaction(selectpictureaction) let capturefromcamera = uialertaction( title: "capture photo camera", style: .default) { (action) -> void in imagefromsource.sourcetype = uiimagepickercontrollersourcetype.camera self.present(imagefromsource, animated: true, completion: nil) } alertcontroller.addaction(capturefromcamera) }
and have function:
func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : anyobject]) { let imageurl = info[uiimagepickercontrollerreferenceurl] as! nsurl let imagename = imageurl.lastpathcomponent let documentdirectory = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true).first! let photourl = nsurl(fileurlwithpath: documentdirectory) let localpath = photourl.appendingpathcomponent(imagename!) let image = info[uiimagepickercontrolleroriginalimage]as! uiimage let data = uiimagepngrepresentation(image) { try data?.write(to: localpath!, options: data.writingoptions.atomic) } catch { // catch exception here , act accordingly } self.dismiss(animated: true, completion: {}) imageview.image = image . . .
when user chooses image gallery - works fine, when user takes photo - app crashes on line:
let imageurl = info[uiimagepickercontrollerreferenceurl] as! nsurl
with error:
fatal error: unexpectedly found nil while unwrapping optional value
i need imageurl display image later on imageview
- how can handle camera output here?
let imageurl = info[uiimagepickercontrollerreferenceurl] as! nsurl
i need imageurl display image later on imageview - how can handle camera output here?
you wrong. not need it, , makes no sense ask it, image, definition, not in photo library — has no reference url.
the key want in situation uiimagepickercontrolleroriginalimage
.
(indeed, in probability should never have been using uiimagepickercontrollerreferenceurl
anything, since given uiimagepickercontrolleroriginalimage
in both cases. that image expected use if purpose display in image view.)
Comments
Post a Comment