這裏是我的代碼如下:基本上我試圖讓用戶選擇相機button1或從照片庫button1中選擇一個選項。然後根據按鈕他們按下它添加到imageview1。然後在相同的視圖控制器上,它被複制到顯示器的下半部分,並帶有一個照相機按鈕2和一個照片庫按鈕2,它們將轉到imageview2。在一個視圖控制器上,您是否有兩個不同的相機按鈕可用於兩個不同的圖像視圖? (Swift 3)
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var ImageView1: UIImageView!
@IBOutlet weak var ImageView2: UIImageView!
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
var imagePicker = UIImagePickerController()
var cameraImagePicker = UIImagePickerController()
var imagePicked = 0
var cameraPicked = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
cameraImagePicker.delegate = self
cameraImagePicker.sourceType = .camera
cameraImagePicker.allowsEditing = false
textField1.delegate = self
textField2.delegate = self
}
@IBAction func uploadImageBtnClick1(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
print("Working")
imagePicked = sender.tag
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
if imagePicked == 0{
ImageView1.image = pickedImage
} else if imagePicked == 1{
ImageView2.image = pickedImage
} else {
print("Something went wrong")
}
if cameraPicked == 2 {
ImageView1.image = pickedImage
} else if cameraPicked == 3 {
ImageView2.image = pickedImage
}
} else {
print("Something went wrong")
}
dismiss(animated: true, completion: nil)
}
@IBAction func CameraButtonTapped(_ sender: UIButton) {
cameraPicked = sender.tag
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
print("working")
self.present(cameraImagePicker, animated: true, completion: nil)
}
}
}
由於您發佈的代碼似乎通常會按照您的要求進行操作,您的問題或問題到底是什麼? – rmaddy
由於某種原因,當我按下相機按鈕2並拍攝照片時,它會填充兩個圖像視圖。圖像似乎在兩個圖像視圖之間跳轉,每次使用相機按鈕時都會更改。 – FreitDev