I want to take a photo using the device's camera.I want the image to be passed to another viewcontroller using segue.The image needs to be shown in the second viewcontroller only.But image needs to be saved to device's photo library.How can I acheive this using the imagepicker?
The code i've tried out is shown below
func takePhoto(sender : UIButton)
{
if (UIImagePickerController.isSourceTypeAvailable(.Camera))
{
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
imagePicker.allowsEditing = false
imagePicker.sourceType = .Camera
imagePicker.cameraCaptureMode = .Photo
presentViewController(imagePicker, animated: true, completion: {})
} else {
print("Rear camera doesn't exist Application cannot access the camera.")
}
} else {
print("Camera inaccessable Application cannot access the camera.")
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?)
{
TakenImage = image
let selectorToCall = Selector("imageWasSavedSuccessfully:didFinishSavingWithError:context:")
UIImageWriteToSavedPhotosAlbum(TakenImage!, self, selectorToCall, nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
print("User canceled image")
dismissViewControllerAnimated(true, completion: {
// Anything you want to happen when the user selects cancel
})
}
TakenImage is passed to next viewcontroller.But the image is not appearing there.The image is not saved library also
The code used for passing is shown below
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
if (indexPath.row == 0)
{
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("NoteDetailViewController") as! NoteDetailViewController
controller.takinPhoto = true
if(noteAlreadyEntered == true)
{
if (note != "")
{
controller.content = note
}
controller.takenImage = TakenImage
if (self.tags != "")
{
controller.tagsTextField.text = self.tags
}
}
else
{
controller.takenImage = TakenImage
if (self.tags != "")
{
controller.tagsTextField.text = self.tags
}
}
self.navigationController!.pushViewController(controller, animated: true)
}
}
How to solve this error?
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
pass the image using prepareforsegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "yoursegueidentifier" {
let dvc = segue.destinationViewController as! yourViewController
dvc.image = image
}
}