Swift - 获取 UIImagePickerController 的发送者
Swift - get sender of UIImagePickerController
我有 2 个 UIImageView,我在其中添加了一个 UITapGestureRecognizer,用于打开用户的相机。一旦用户选择了图像,为了访问相机而点击的 UIImageView 应该设置为相机图像。
这是我的 openCamera 方法:
@objc func openCamera(sender: ImagePlaceHolderView) {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
picker.sourceType = .camera
present(picker, animated: true)
}
我的 UIImageView 声明如下:
var left = UIImageView()
var right = UIImageView()
left = createImagePlaceholder()
right = createImagePlaceholder()
private func createImagePlaceholder() -> UIImageView {
let placeholder = ImagePlaceHolderView(frame: CGRect(x: 0, y: 0, width: 90, height: 150))
placeholder.isUserInteractionEnabled = true
return placeholder
}
然后是我的委托方法:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
left.image = image // Here I want to set the sender's image to image
dismiss(animated: true, completion: nil)
}
我正在努力弄清楚如何将点击的 UIImageView 的图像设置为来自相机的新图像。我试图将发件人传递给 openCamera 方法,但不确定如何提取它,因为发件人实际上似乎是 UITapGestureRecognizer 而不是被点击的图像。
您必须存储对 sender
的引用,并使用它在委托方法中设置图像。
var imagePlaceholderView: ImagePlaceHolderView? // set the reference
@objc func openCamera(sender: UITapGestureRecognizer) {
imagePlaceholderView = sender.view as? ImagePlaceHolderView
//...
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
imagePlaceholderView?.image = image
dismiss(animated: true, completion: nil)
}
我有 2 个 UIImageView,我在其中添加了一个 UITapGestureRecognizer,用于打开用户的相机。一旦用户选择了图像,为了访问相机而点击的 UIImageView 应该设置为相机图像。
这是我的 openCamera 方法:
@objc func openCamera(sender: ImagePlaceHolderView) {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
picker.sourceType = .camera
present(picker, animated: true)
}
我的 UIImageView 声明如下:
var left = UIImageView()
var right = UIImageView()
left = createImagePlaceholder()
right = createImagePlaceholder()
private func createImagePlaceholder() -> UIImageView {
let placeholder = ImagePlaceHolderView(frame: CGRect(x: 0, y: 0, width: 90, height: 150))
placeholder.isUserInteractionEnabled = true
return placeholder
}
然后是我的委托方法:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
left.image = image // Here I want to set the sender's image to image
dismiss(animated: true, completion: nil)
}
我正在努力弄清楚如何将点击的 UIImageView 的图像设置为来自相机的新图像。我试图将发件人传递给 openCamera 方法,但不确定如何提取它,因为发件人实际上似乎是 UITapGestureRecognizer 而不是被点击的图像。
您必须存储对 sender
的引用,并使用它在委托方法中设置图像。
var imagePlaceholderView: ImagePlaceHolderView? // set the reference
@objc func openCamera(sender: UITapGestureRecognizer) {
imagePlaceholderView = sender.view as? ImagePlaceHolderView
//...
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
imagePlaceholderView?.image = image
dismiss(animated: true, completion: nil)
}