enter image description here我正在制作约会应用。 因此用户需要在个人资料中更新他们的图像。 在个人资料部分, 有3个小图像视图。 如果他们想要为每个图像视图添加图像, 他们应该通过UIimagepicker更新每个图像视图。 但我知道我只能在UIImagepikerControllerDelegate协议中只使用一个函数。
我该怎么做?
下面的是我失败的代码
导入UIKit
class RegisterPicture:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
@IBAction func pick1(sender: AnyObject) {
let picker1 = UIImagePickerController()
picker1.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker1.allowsEditing = true
picker1.delegate = self
self.presentViewController(picker1, animated: false, completion: nil)
}
@IBAction func pick2(sender: AnyObject) {
let picker2 = UIImagePickerController()
picker2.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker2.allowsEditing = true
picker2.delegate = self
self.presentViewController(picker2, animated: false, completion: nil)
}
@IBAction func pick3(sender: AnyObject) {
let picker = UIImagePickerController()
picker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker.allowsEditing = true
picker.delegate = self
self.presentViewController(picker, animated: false, completion: nil)
}
@IBOutlet var picture1: UIImageView!
@IBOutlet var picture2: UIImageView!
@IBOutlet var picture3: UIImageView!
func imagePickerController(picker1: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker1.dismissViewControllerAnimated(false, completion : nil)
}
func imagePickerController(picker2: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker2.dismissViewControllerAnimated(false, completion : nil)
self.picture2.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerController(picker3: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker3.dismissViewControllerAnimated(false, completion : nil)
self.picture3.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(false, completion:nil)
}
func imagePickerControllerDidCancel(picker2: UIImagePickerController) {
picker2.dismissViewControllerAnimated(false, completion:nil)
}
func imagePickerControllerDidCancel(picker3: UIImagePickerController) {
picker3.dismissViewControllerAnimated(false, completion:nil)
}
答案 0 :(得分:0)
不确定我是否理解了您的需求,但我建议那些UIImageViews可以在集合视图中。
通过这样做,图像选择器的唯一代理将是视图控制器,其中包含图像的集合视图,您只需使用拾取的图像更新选定的单元格
答案 1 :(得分:0)
在创建imageView和时,您可以为每个imageView提供标记 在这个方法里面
-(void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info
在if else条件的帮助下,您可以单独使用每个imageView。
答案 2 :(得分:0)
您可以将其创建为不处理已分配操作的新项目 - 或者您可以清除视图中的操作和出口,并根据新代码重新创建。
创建3个UIImageViews,picture1,picture2,picture3。链接代码中的插座。
创建一个包含3个UIBarButtonItems的UIToolbar。将条形按钮项标签设置为1,2,3,对应于picture1,picture2,picture3图像视图。
连接图像视图的出口(图片1,2,3)
将选择器操作连接到所有三个栏按钮项。这些将使用相同的操作,无需重复代码,发件人按钮的标签将检测到差异。
按照代码,它会解释自己。它根据点击的条形按钮决定在图像选择期间哪个UIImageView将是“活动的”,然后委托将使用该图像视图。
如果没有意义,请告诉我们。
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBAction func picker(sender: UIBarButtonItem) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .SavedPhotosAlbum
switch sender.tag {
case 2:
self.imageView = picture2
case 3:
self.imageView = picture3
default:
self.imageView = picture1
}
presentViewController(imagePicker, animated: true, completion: nil)
}
@IBOutlet weak var picture1: UIImageView!
@IBOutlet weak var picture2: UIImageView!
@IBOutlet weak var picture3: UIImageView!
var imageView: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker.delegate = self
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.imageView.contentMode = .ScaleAspectFit
self.imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
编辑:这是我刚刚上传的示例项目: https://github.com/smozgur/Multiple-Image-Picker