[英]I want to select image using two buttons and want to display those images on two different image view in swift..how could I do this.?
I want to select image using two buttons and want to display those images on two different image view in swift 我想使用两个按钮选择图像,并想在两个不同的图像视图中快速显示这些图像
func pickimage(){
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let controller = UIImagePickerController()
controller.delegate = self
controller.sourceType = .photoLibrary
controller.allowsEditing = false
present(controller, animated: true, completion: nil)
}
}
Image Picker Delegates 图像选择器代表
extension AdnewDOC : UIImagePickerControllerDelegate,UINavigationControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
frontview.image = image
frontview.contentMode = .scaleAspectFill
dismiss(animated: true, completion: nil)
}
}
You can use objective-c
associated types objc_setAssociatedObject
and objc_getAssociatedObject
like this: . 您可以像这样使用与objective-c
相关的类型objc_setAssociatedObject
和objc_getAssociatedObject
: 。
I have updated your method, Please pass your image view obejct (which image view you want to show image) from button action method. 我已经更新了您的方法,请通过按钮操作方法传递您的图像视图对象(您要显示图像的图像视图)。 Like a demo action method for buttons: 就像按钮的演示动作方法一样:
import ObjectiveC
var myKey = "myObjectKey"
@IBAction func firstButtonAction(_ sender: Any) {
pickimage(imageView: firstImageView)
}
@IBAction func secondButtonAction(_ sender: Any) {
pickimage(imageView: secondImageView)
}
func pickimage(imageView: UIImageView){
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let controller = UIImagePickerController()
objc_setAssociatedObject(controller, &myKey, imageView, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
controller.delegate = self
controller.sourceType = .photoLibrary
controller.allowsEditing = false
present(controller, animated: true, completion: nil)
}
}
And get the image object in delegate method like this: 并在委托方法中获取图像对象,如下所示:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let imageView = objc_getAssociatedObject(picker, &myKey) as! UIImageView
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = image
imageView.contentMode = .scaleAspectFill
dismiss(animated: true, completion: nil)
}
For more understanding how to work
objc_setAssociatedObject
andobjc_getAssociatedObject
please check below links: 为了进一步了解objc_setAssociatedObject
和objc_getAssociatedObject
工作objc_setAssociatedObject
,请检查以下链接:http://blog.corywiles.com/objective-c-runtime-with-swfit http://blog.corywiles.com/objective-c-runtime-with-swfit
http://en.swifter.tips/associated-object/ http://en.swifter.tips/associated-object/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.