使用不同按钮在一个视图控制器中上传多个图像(iOS、Xcode9、Swift4)
Multiple Image uploads in One View Controller with different Buttons (iOS, Xcode9, Swift4)
这里有 4 个 UIImageView,每个 UIImageView 有 4 个单独的按钮用于图像上传。当用户单击每个按钮时,我无法修复必须上传适当图像的问题。意味着当用户点击后退按钮时,必须上传适当的图像。我附上了我的 swift 代码,请帮助修复
var imagePicker = UIImagePickerController()
var imagePicked = 0
@IBOutlet weak var adImage1: UIImageView!
@IBOutlet weak var adImage2: UIImageView!
@IBOutlet weak var adImage3: UIImageView!
@IBOutlet weak var adImage4: UIImageView!
@IBAction func uploadImage1(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func uploadImage2(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker1 = UIImagePickerController()
imagePicker1.delegate = self
imagePicker1.sourceType = .photoLibrary;
imagePicker1.allowsEditing = true
self.present(imagePicker1, animated: true, completion: nil)
} }
@IBAction func uploadImage3(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker2 = UIImagePickerController()
imagePicker2.delegate = self
imagePicker2.sourceType = .photoLibrary;
imagePicker2.allowsEditing = true
self.present(imagePicker2, animated: true, completion: nil)
} }
@IBAction func uploadImage4(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker3 = UIImagePickerController()
imagePicker3.delegate = self
imagePicker3.sourceType = .photoLibrary;
imagePicker3.allowsEditing = true
self.present(imagePicker3, animated: true, completion: nil)
} }
internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let imagePicker = info[UIImagePickerControllerOriginalImage] as? UIImage
if imagePicked == 1 {
adImage1.image = imagePicker
}
else if imagePicked == 2
{
adImage2.image = imagePicker
}
else if imagePicked == 3
{
adImage3.image = imagePicker
}
else if imagePicked == 4
{
adImage4.image = imagePicker
} else
{
// Error message
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true)
}
更改您的按钮 IBaction 代码以设置根据按钮点击选取的图像,以便您的代码像魅力一样工作
@IBAction func uploadImage1(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 1
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func uploadImage2(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 2
let imagePicker1 = UIImagePickerController()
imagePicker1.delegate = self
imagePicker1.sourceType = .photoLibrary;
imagePicker1.allowsEditing = true
self.present(imagePicker1, animated: true, completion: nil)
}
}
@IBAction func uploadImage3(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 3
let imagePicker2 = UIImagePickerController()
imagePicker2.delegate = self
imagePicker2.sourceType = .photoLibrary;
imagePicker2.allowsEditing = true
self.present(imagePicker2, animated: true, completion: nil)
}
}
@IBAction func uploadImage4(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 4
let imagePicker3 = UIImagePickerController()
imagePicker3.delegate = self
imagePicker3.sourceType = .photoLibrary;
imagePicker3.allowsEditing = true
self.present(imagePicker3, animated: true, completion: nil)
}
}
这里有 4 个 UIImageView,每个 UIImageView 有 4 个单独的按钮用于图像上传。当用户单击每个按钮时,我无法修复必须上传适当图像的问题。意味着当用户点击后退按钮时,必须上传适当的图像。我附上了我的 swift 代码,请帮助修复
var imagePicker = UIImagePickerController()
var imagePicked = 0
@IBOutlet weak var adImage1: UIImageView!
@IBOutlet weak var adImage2: UIImageView!
@IBOutlet weak var adImage3: UIImageView!
@IBOutlet weak var adImage4: UIImageView!
@IBAction func uploadImage1(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func uploadImage2(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker1 = UIImagePickerController()
imagePicker1.delegate = self
imagePicker1.sourceType = .photoLibrary;
imagePicker1.allowsEditing = true
self.present(imagePicker1, animated: true, completion: nil)
} }
@IBAction func uploadImage3(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker2 = UIImagePickerController()
imagePicker2.delegate = self
imagePicker2.sourceType = .photoLibrary;
imagePicker2.allowsEditing = true
self.present(imagePicker2, animated: true, completion: nil)
} }
@IBAction func uploadImage4(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker3 = UIImagePickerController()
imagePicker3.delegate = self
imagePicker3.sourceType = .photoLibrary;
imagePicker3.allowsEditing = true
self.present(imagePicker3, animated: true, completion: nil)
} }
internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let imagePicker = info[UIImagePickerControllerOriginalImage] as? UIImage
if imagePicked == 1 {
adImage1.image = imagePicker
}
else if imagePicked == 2
{
adImage2.image = imagePicker
}
else if imagePicked == 3
{
adImage3.image = imagePicker
}
else if imagePicked == 4
{
adImage4.image = imagePicker
} else
{
// Error message
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true)
}
更改您的按钮 IBaction 代码以设置根据按钮点击选取的图像,以便您的代码像魅力一样工作
@IBAction func uploadImage1(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 1
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func uploadImage2(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 2
let imagePicker1 = UIImagePickerController()
imagePicker1.delegate = self
imagePicker1.sourceType = .photoLibrary;
imagePicker1.allowsEditing = true
self.present(imagePicker1, animated: true, completion: nil)
}
}
@IBAction func uploadImage3(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 3
let imagePicker2 = UIImagePickerController()
imagePicker2.delegate = self
imagePicker2.sourceType = .photoLibrary;
imagePicker2.allowsEditing = true
self.present(imagePicker2, animated: true, completion: nil)
}
}
@IBAction func uploadImage4(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicked = 4
let imagePicker3 = UIImagePickerController()
imagePicker3.delegate = self
imagePicker3.sourceType = .photoLibrary;
imagePicker3.allowsEditing = true
self.present(imagePicker3, animated: true, completion: nil)
}
}