如何拍照并将其传递给另一个 viewcontroller
How to take a photo and pass it to another viewcontroller
我想使用设备的camera.I拍照希望将图像传递给另一个viewcontroller使用segue.The图像需要显示在第二个viewcontroller only.But 图像需要保存到设备的照片中 library.How 我可以使用图像选择器实现吗?
我试过的代码如下所示
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 传递给下一个 viewcontroller.But 图像未出现 there.The 图像也未保存库
用于传递的代码如下所示
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)
}
}
如何解决这个错误?
您需要先确定图像是被单击还是从图库中选择的。如果单击将 bool 值更改为 yes 并在其中使用 underwritten code 。这会将您的图像保存在图库中:
func imagePickerController(选择器:UIImagePickerController,didFinishPickingMediaWithInfo 信息:[String:AnyObject])
{
选择器.dismissViewControllerAnimated(真,完成:无)
imageCLicked = info[UIImagePickerControllerOriginalImage] as? UIImage
让 imageData = UIImageJPEGRepresentation(imageCLicked, 0.6)
让 compressedJPGImage = UIImage(data: imageData!)
ALAssetsLibrary().writeImageToSavedPhotosAlbum(compressedJPGImage!.CGImage, orientation: ALAssetOrientation(rawValue: compressedJPGImage!.imageOrientation.rawValue)!,
completionBlock:{ (path:NSURL!, error:NSError!) -> 无效
print("(path)") //在这里你会得到你的路径
})
}
希望对你有用。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
使用 prepareforsegue 传递图像:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "yoursegueidentifier" {
let dvc = segue.destinationViewController as! yourViewController
dvc.image = image
}
}
我想使用设备的camera.I拍照希望将图像传递给另一个viewcontroller使用segue.The图像需要显示在第二个viewcontroller only.But 图像需要保存到设备的照片中 library.How 我可以使用图像选择器实现吗?
我试过的代码如下所示
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 传递给下一个 viewcontroller.But 图像未出现 there.The 图像也未保存库
用于传递的代码如下所示
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)
}
}
如何解决这个错误?
您需要先确定图像是被单击还是从图库中选择的。如果单击将 bool 值更改为 yes 并在其中使用 underwritten code 。这会将您的图像保存在图库中:
func imagePickerController(选择器:UIImagePickerController,didFinishPickingMediaWithInfo 信息:[String:AnyObject]) { 选择器.dismissViewControllerAnimated(真,完成:无)
imageCLicked = info[UIImagePickerControllerOriginalImage] as? UIImage
让 imageData = UIImageJPEGRepresentation(imageCLicked, 0.6) 让 compressedJPGImage = UIImage(data: imageData!) ALAssetsLibrary().writeImageToSavedPhotosAlbum(compressedJPGImage!.CGImage, orientation: ALAssetOrientation(rawValue: compressedJPGImage!.imageOrientation.rawValue)!, completionBlock:{ (path:NSURL!, error:NSError!) -> 无效 print("(path)") //在这里你会得到你的路径 }) }
希望对你有用。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
使用 prepareforsegue 传递图像:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "yoursegueidentifier" {
let dvc = segue.destinationViewController as! yourViewController
dvc.image = image
}
}