我想使用设备的相机拍摄照片。我希望使用segue将图像传递到另一个viewcontroller。图像只需要显示在第二个viewcontroller中。但是图像需要保存到设备的照片库中。我怎样才能使用imagepicker来实现这个目标?
我试过的代码如下所示
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。但是图像没有出现在那里。图像也没有保存库
用于传递的代码如下所示
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)
}
}
如何解决此错误?
答案 0 :(得分:1)
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
}
}
答案 1 :(得分:0)
如果从图库中点击或选择图像,则需要先识别。如果单击将bool值更改为yes,并使用underwriting代码。这会将您的图片保存在图库中:
func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo info:[String:AnyObject]) { picker .dismissViewControllerAnimated(true,completion:nil)
imageCLicked = info[UIImagePickerControllerOriginalImage] as? UIImage
让imageData = UIImageJPEGRepresentation(imageCLicked,0.6) let compressedJPGImage = UIImage(data:imageData!) ALAssetsLibrary()。writeImageToSavedPhotosAlbum(compressedJPGImage!.CGImage,orientation:ALAssetOrientation(rawValue:compressedJPGImage!.imageOrientation.rawValue)! completionBlock:{(路径:NSURL!,错误:NSError!) - >无效 print(“(path)”)//在这里,你将获得你的路径 }) }
希望它适合你。