[英]moving the UIImageView from the main view to another
my code works fine in choosing the picture from the gallery and display it in the same view, what am stuck into now, is transferring that UIImageView chosen to the next activity when "next" button is clicked 我的代码可以从画廊中选择图片并将其显示在同一视图中,现在仍然可以正常工作,即单击“下一步”按钮时将选择的UIImageView转移到下一个活动
here is the code that opens the gallery 这是打开图库的代码
@IBAction func gallery(sender: AnyObject) {
if UIImagePickerController.availableMediaTypesForSourceType(.PhotoLibrary) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
}
this is the code that displays the image in the same view 这是在同一视图中显示图像的代码
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageChosen.contentMode = .ScaleAspectFit
imageChosen.image = chosenImage
dismissViewControllerAnimated(true, completion: nil)
}
now after the UIImageView saved in the imageChosen, here is the code am using to pass that image to the next view 现在将UIImageView保存在imageChosen中之后,这是代码用来将该图像传递到下一个视图
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var pass:postView = segue.destinationViewController as! postView
if(segue.identifier == "next"){
pass.imgv.image = imageChosen.image
}
}
this line of code that causes the program to crash 这行代码导致程序崩溃
pass.imgv.image = imageChosen.image
the imgv in the second view is declared like this 第二个视图中的imgv像这样声明
@IBOutlet weak var imgv: UIImageView!
what am i doing wrong here, please direct me 我在这里做错什么,请指导我
You cannot set data in a view which is not rendered yet . 您不能在尚未渲染的视图中设置数据。 So pass image to second view and set that Image to ImageView in secondView's viewDidLoad
因此,将图像传递到第二个视图,然后在secondView的viewDidLoad中将该图像设置为ImageView
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
if segue.identifier == "next" {
var pass:second = segue.destinationViewController as! second
pass.currentImage=myImageView.image;
}
}
//Second view //第二个视图
class second: UIViewController {
@IBOutlet weak var tempImgView: UIImageView!
var currentImage:UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if((currentImage) != nil){
tempImgView.image=currentImage;
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You can't access the UIImageView
directly in second View Controller. 您不能直接在第二个View Controller中访问
UIImageView
。 Instead create variable UIImage
in second View Controller and assign your selected UIImage
to it. 而是在第二个View Controller中创建变量
UIImage
并将您选择的UIImage
分配给它。 Later in viewdidload
of second View Controller set the UIImageView
. 稍后在第二个View Controller的
viewdidload
中设置UIImageView
。
First View 初见
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let pass:postView = segue.destinationViewController as! postView
if(segue.identifier == "next"){
pass.tempImage = imageChosen.image
}
}
Second View 第二种观点
@IBOutlet weak var imgv: UIImageView!
var tempImage:UIImage!
override func viewDidLoad() {
super.viewDidLoad()
self.imgv.image=tempImage
}
You are using weak reference for image view and trying to get it in other controllers . 您正在使用弱引用进行图像查看,并尝试在其他控制器中获取它。 do as following .
请执行以下操作。
@IBOutlet Strong var imgv: UIImageView @IBOutlet强变体imgv:UIImageView
Secondly your way of fetching view controller wrong ... Try it as following 其次,您错误地获取视图控制器的方法如下:
if segue.identifier == "ShowCounterSegue"
{
if let destinationVC = segue.destinationViewController as? OtherViewController{
destinationVC.numberToDisplay = counter
}
}
In the PostView, just create variable like var img: UIImage!
在PostView中,只需创建类似
var img: UIImage!
变量即可var img: UIImage!
Replace this line pass.imgv.image = imageChosen.image
with pass.img = imageChosen.image
将此行
pass.imgv.image = imageChosen.image
替换为pass.img = imageChosen.image
In viewDidLoad() of PostView, add this line, imgv.image = image
在PostView的viewDidLoad()中,添加以下行,
imgv.image = image
We can not set properties of IBOutlets
of secondView from the firstView, because IBOutlets of ViewController will get memory after execution of viewDidLoad()
我们无法从firstView设置secondView的
IBOutlets
的属性,因为ViewController的IBOutlets 将在执行viewDidLoad()
之后获取内存。
So you have to create appropriate data variable to pass data to IBOutlet. 因此,您必须创建适当的数据变量以将数据传递到IBOutlet。
Here is the example code. 这是示例代码。
class FirstViewController: UIViewController {
@IBOutlet weak var imageViewInput: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//check the condition for your segue.identifier, if any
var destination : NewViewContrller! = segue.destinationViewController as NewViewContrller ;
if let image = imageViewInput?.image {
destination.outputImage = image;
}
}
}
class NewViewContrller: UIViewController {
@IBOutlet weak var outputImageView: UIImageView!
var outputImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let image = outputImage {
outputImageView.image = image;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.