2016-08-26 8 views
0

私は2つのボタンを持つ2つのUIImageViewを持っています。 1つのボタンは写真を撮り、もう1つは図書館から写真を選ぶ。両方のボタンが正しく機能します。しかし、最初のシートからボタンで画像を選択すると、UIImageViewの両方に表示されます。私の質問は、対応するImageView?にのみ画像を表示する方法です。didFinishPickingImageに2つの画像を表示する - iOS Swift 2.2

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) { 
    print("Image Selected") 
    self.dismissViewControllerAnimated(true, completion: nil) 

    importedImage.image = image 
    secondPhoto.image = image 
} 

// MARK: - Action Sheet 


@IBAction func showActionSheet1(sender: AnyObject) { 
    let image = UIImagePickerController() 
    image.delegate = self 
    let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet) 
    let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in 
     image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
     image.allowsEditing = false 

     self.presentViewController(image, animated: true, completion: nil) 

    }) 
    let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in 

     if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){ 

     image.sourceType = UIImagePickerControllerSourceType.Camera 
     image.allowsEditing = false 
     self.presentViewController(image, animated: true, completion: nil) 
     } 
    }) 
    let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in 
     print("Cancel selected") 
    }) 
    actionSheet.addAction(libButton) 
    actionSheet.addAction(cameraButton) 
    actionSheet.addAction(cancelButton) 


    self.presentViewController(actionSheet, animated: true, completion: nil) 

} 

@IBAction func showActionSheet2(sender: AnyObject) { 

    let image = UIImagePickerController() 
    image.delegate = self 

    let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet) 
    let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in 
     image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
     image.allowsEditing = false 
     self.presentViewController(image, animated: true, completion: nil) 

    }) 
    let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in 

     if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){ 

      image.sourceType = UIImagePickerControllerSourceType.Camera 
      image.allowsEditing = false 
      self.presentViewController(image, animated: true, completion: nil) 
     } 
    }) 
    let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in 
     print("Cancel selected") 
    }) 
    actionSheet.addAction(libButton) 
    actionSheet.addAction(cameraButton) 
    actionSheet.addAction(cancelButton) 


    self.presentViewController(actionSheet, animated: true, completion: nil) 
} 
+0

変更したいimageViewのグローバル参照を使用します。アクションシート関数で、その変数を目的のimageViewに設定します。 – brandonscript

+0

あなたの迅速な対応に感謝します。問題が解決しました。私はvar imageInputを追加しました:UIImageView !. presentViewControllerの前のアクションシートの中で、 "self.imageInput = self.importedImage"を設定します。 didFinishPickingImageメソッドでは、 "input.image = image"に変更します。 –

答えて

0

あなたはdidFinishPickingImage内部ImageViewの両方に画像を設定しているので、これは問題を解決するために、何が起こっているあなたは1つのBoolインスタンスを作成して割り当てることができるため、ButtonImageを設定するためにクリックされたようないくつかの状態を維持する必要がありますこのようなButtonのアクション内の値。

var isFromFirst: Bool = false 

@IBAction func showActionSheet1(sender: AnyObject) { 

    self.isFromFirst = true 
    ...//your other code 

} 

@IBAction func showActionSheet2(sender: AnyObject) { 

    self.isFromFirst = false 
    ...//your other code 

} 

この後、このブール値をdidFinishPickingImageのようにチェックします。

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) { 
    print("Image Selected") 
    self.dismissViewControllerAnimated(true, completion: nil) 
    if (self.isFromFirst) { 
     importedImage.image = image 
    } 
    else { 
     secondPhoto.image = image 
    } 
} 
関連する問題