2017-05-03 8 views
-2

私はUIImagePickerViewControllerで2つの異なる画像を選択するために、2つのUIImageViewsと2つのボタンを持つビューを持っています。実際には動作しますが、1枚の画像をアップロードすると、もう1枚のUIView Imageも同じ画像を選択しています。写真を選択した後にどの画像ビューを更新するのかを知るにはどうすればよいですか?

私は間違っていますか?

} 

@IBAction func uploadButton(_ sender: Any) { 

    let imagePickerController = UIImagePickerController() 
    imagePickerController.delegate = self 

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet) 

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in 

     if UIImagePickerController.isSourceTypeAvailable(.camera) { 
      imagePickerController.sourceType = .camera 
      self.present(imagePickerController, animated: true, completion: nil) 
     }else{ 
      print("Camera not available") 
     } 


    })) 

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in 
     imagePickerController.sourceType = .photoLibrary 
     self.present(imagePickerController, animated: true, completion: nil) 
    })) 

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 

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


} 

@IBAction func coverButton(_ sender: Any) { 

    let imagePickerController = UIImagePickerController() 
    imagePickerController.delegate = self 

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet) 

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in 

     if UIImagePickerController.isSourceTypeAvailable(.camera) { 
      imagePickerController.sourceType = .camera 
      self.present(imagePickerController, animated: true, completion: nil) 
     }else{ 
      print("Camera not available") 
     } 


    })) 

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in 
     imagePickerController.sourceType = .photoLibrary 
     self.present(imagePickerController, animated: true, completion: nil) 
    })) 

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 

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




} 
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage 

    profileImage.image = image 
    backgroundImage.image = image 

    picker.dismiss(animated: true, completion: nil) 

} 

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
    picker.dismiss(animated: true, completion: nil) 

} 
+0

あなたは常にimagePickerController didFinishPickingMediaWithInfo方法で両方のビューに画像を設定します。 – Vladimir

+0

代わりに何を代わりにして、お返事いただきありがとうございます – RedR4nger

答えて

1

コードが重複しており、画像の選択後にどの画像ビューを更新するかを決めることはできません。コードをリファクタリングする必要があります。

は、あなたのクラスにプロパティを追加します。

var chosenImageView: UIImageView? 

その後、独自の方法にあなたの共通のコードを置く:何のカメラが存在しない場合は、カメラボタンが表示されていないか

function selectPhoto() { 
    let imagePickerController = UIImagePickerController() 
    imagePickerController.delegate = self 

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet) 

    if UIImagePickerController.isSourceTypeAvailable(.camera) { 
     actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in 
      imagePickerController.sourceType = .camera 
      self.present(imagePickerController, animated: true, completion: nil) 
     })) 
    }else{ 
     print("Camera not available") 
    } 

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in 
     imagePickerController.sourceType = .photoLibrary 
     self.present(imagePickerController, animated: true, completion: nil) 
    })) 

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 

    self.present(actionSheet, animated: true, completion: nil) 
} 

は注意してください。

その後、あなたの二つの動作をリファクタリング:

@IBAction func uploadButton(_ sender: Any) { 
    chosenImageView = profileImage 

    selectPhoto() 
} 

@IBAction func coverButton(_ sender: Any) { 
    chosenImageView = backgroundImage 

    selectPhoto() 
} 

そして最後にデリゲートメソッド:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage 

    chosenImageView?.image = image 

    picker.dismiss(animated: true, completion: nil) 
} 

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
    picker.dismiss(animated: true, completion: nil) 
} 
+0

おかげでたくさんありがとう – RedR4nger

関連する問題