[英]Can you have two different camera buttons for two different image views on one view controller? (Swift 3)
Here is my code below: Basically I am trying to give the user an option to choose the camera button1 or choose from photo library button1. 这是下面的代码:基本上,我试图为用户提供一个选择相机按钮1或从照片库按钮1中进行选择的选项。 Then depending on the button they press it adds to imageview1.
然后,根据他们按下的按钮,将其添加到imageview1。 Then on the same view controller it is duplicated on the bottom half of the display with a camera button2 and a photo library button 2 that will go to the imageview2.
然后,在同一视图控制器上,将其复制到显示屏的下半部分,并带有将进入imageview2的相机按钮2和照片库按钮2。
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)
}
}
}
The problem is your use of two different variables to track which image view should be updated. 问题是您使用两个不同的变量来跟踪应更新哪个图像视图。 Either reset both variables after updating an image view or use only one variable.
更新图像视图后重置两个变量,或仅使用一个变量。
And there is no need for two UIImagePickerController
instances. 并且不需要两个
UIImagePickerController
实例。
Lastly, update the tags of all 4 buttons. 最后,更新所有4个按钮的标签。 For the two buttons that affect
ImageView1
, set their tag to 1. For the two buttons that affect ImageView2
, set their tag to 2. Never use a tag of 0
since all views have a default tag of 0
. 对于影响两个按钮
ImageView1
,设置它们的标签1.对于影响两个按钮ImageView2
,设置它们的标签2.请勿使用的标签0
,因为所有的观点有一个默认标签0
。
BTW - name variables and functions with a leading lowercase letter. BTW-用前导小写字母命名变量和函数。 Only class and struct names should start with uppercase.
只有类和结构名才能以大写字母开头。
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 imagePicked = 0
override func viewDidLoad() {
super.viewDidLoad()
textField1.delegate = self
textField2.delegate = self
}
@IBAction func uploadImageBtnClick1(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
print("Working")
imagePicked = sender.tag
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func cameraButtonTapped(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
print("working")
imagePicked = sender.tag
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
if imagePicked == 1 {
imageView1.image = pickedImage
} else if imagePicked == 2 {
imageView2.image = pickedImage
} else {
print("Something went wrong")
}
} else {
print("Something went wrong")
}
dismiss(animated: true, completion: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.