Ios 如何拍摄照片并将其传递给另一个viewcontroller
我想使用设备的照相机拍摄照片。我想使用segue将图像传递给另一个viewcontroller。图像只需在第二个viewcontroller中显示。但图像需要保存到设备的照片库。如何使用imagepicker实现此目的 我试用过的代码如下所示Ios 如何拍摄照片并将其传递给另一个viewcontroller,ios,swift,uiimagepickercontroller,photos,Ios,Swift,Uiimagepickercontroller,Photos,我想使用设备的照相机拍摄照片。我想使用segue将图像传递给另一个viewcontroller。图像只需在第二个viewcontroller中显示。但图像需要保存到设备的照片库。如何使用imagepicker实现此目的 我试用过的代码如下所示 func takePhoto(sender : UIButton) { if (UIImagePickerController.isSourceTypeAvailable(.Camera)) { if UIImagePi
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
})
}
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)
}
}
TakenImage被传递到下一个viewcontroller。但是图像没有出现在那里。图像也没有保存到库中
用于传递的代码如下所示
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
})
}
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)
}
}
如何解决此错误?您需要首先确定图像是单击的还是从“多媒体资料”中选择的。如果单击,则将布尔值更改为“是”,并在其中使用承保代码。这将在gallery中保存您的图像: func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo:[字符串:任意对象]) { picker.dismissViewControllerAnimated(true,完成:nil) 让imageData=UIImageJPEG表示(imageCLicked,0.6) 让compressedJPGImage=UIImage(数据:imageData!) AlasSetLibrary().writeImageToSavedPhotosAlbum(CompressedJjpgImage!.CGImage,方向:ALAssetOrientation(rawValue:CompressedJjpgImage!.imageOrientation.rawValue)!, completionBlock:{(路径:NSURL!,错误:NSError!)->中的Void 打印(“(路径)”//在这里您将获得您的路径 }) } 希望对你有用
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
}
}
你在这方面需要什么帮助?这个问题太宽泛了。请尝试一些方法,并将您的问题更新为一个单一的、有重点的问题,您需要帮助。您可以用传递int的相同方式传递图像吗?