2017-05-03 61 views
-2

我有一个2 UIImageViews和2按钮的视图,以便用UIImagePickerViewController选择2个不同的图片。它实际上工作,但是当我上传1图像时,另一个UIView图像也选择了相同的图像。如何在选择照片后知道要更新哪个图像视图?

我在做什么错?

} 

@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

相关问题