2016-08-09 4 views
1

デバイスのカメラを使って写真を撮りたいです。画像を別のビューコントローラに渡したいのですが、画像は2番目のビューコントローラにのみ表示する必要があります。しかし、イメージはデバイスの写真ライブラリに保存する必要があります。イメージピッカーを使用してこれを達成するにはどうすればよいですか?写真を撮って別のビューコントローラに渡す方法

私が試したコードは

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下に表示され

は次の画像でも

コード使用されるライブラリに保存されませんthere.The画像を表示されないviewcontroller.Butに渡され 合格のために以下に示す

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

を?この質問ははるかに広いです。何かを試して、あなたの質問を更新してください。 – rmaddy

+0

intを渡すのと同じ方法で画像を渡すことができますか? –

答えて

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 
     } 
    } 
0

画像がギャラリーからクリックされたり選択されている場合は、最初に識別する必要があります。クリックされた場合は、boolの値をyesに変更して、そのコードを使用します。このギャラリーに画像を保存します:

FUNCのimagePickerController(ピッカー:UIImagePickerControllerを、didFinishPickingMediaWithInfo情報を:[文字列:ANYOBJECT]) { ピッカー.dismissViewControllerAnimated(真、完了:ゼロ)

imageCLicked = info[UIImagePickerControllerOriginalImage] as? UIImage 

画像データを聞かせて= UIImageJPEGRepresentation(imageCLicked、0.6) compressedJPGImage = UIImage(データ!:画像データ)しましょう。! ALAssetsLibraryを()writeImageToSavedPhotosAlbum(compressedJPGImage .CGImage、オリエンテーション:ALAssetOrientation(rawValue:!compressedJPGImage .imageOrientation.rawValue)! completionBlock: - ) }

それはあなたのために働くホープ{(パス:NSURL!エラー!NSError) 印刷中>ボイド( "(パス)")//ここでは がパスを取得します}。

関連する問題