How to Change the Image Displayed in a Uiimageview Programmatically

how to change uiimageview image's programmatically

try like this ,

for (int i=0; i<20; i++) {

productID=[products objectAtIndex:i];

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
//[button setImage:redButtonImage forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = 100+productID;
[scrollView addSubview:button];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=productID;
UIImage *image = [UIImage imageNamed:@"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];

}

keep this one in button action method and pass the imageview tag in the place of imgview.tag

     UIImageView *img=(UIImageView *)[scrollView viewWithTag:imgview.tag];
img.image=[UIImage imageNamed:@"name.png"];

Programmatically set image to UIImageView with Xcode 6.1/Swift

Since you have your bgImage assigned and linked as an IBOutlet, there is no need to initialize it as a UIImageView... instead all you need to do is set the image property like bgImage.image = UIImage(named: "afternoon"). After running this code, the image appeared fine since it was already assigned using the outlet.

Sample Image

However, if it wasn't an outlet and you didn't have it already connected to a UIImageView object on a storyboard/xib file, then you could so something like the following...

class ViewController: UIViewController {
var bgImage: UIImageView?

override func viewDidLoad() {
super.viewDidLoad()

var image: UIImage = UIImage(named: "afternoon")!
bgImage = UIImageView(image: image)
bgImage!.frame = CGRectMake(0,0,100,200)
self.view.addSubview(bgImage!)
}
}

How to change image of UIImageView that is inside a UIButton programmatically?

you can:

1 - keep a reference to the UIImageView inside the UIButton on your viewController

2 - Subclass the UIButton and add the UIImageView yourself.

3 - When adding the UIImageView, add a tag into it (view.tag) and when getting the view from the sender, you can just

UIView *view = (UIView *)sender;
UIImageView *imageView = [view viewWithTag:123];
//do what you must with the imageView.

The tag can be any number.

Edit

this is how you should set the tag on the UIImageView, and Not on the UIButton. I copied this code from Tim's answer from here:

// Create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// Now load the image and create the image view
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(/*frame*/)];
[imageView setImage:image];

// Create the label and set its text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(/*frame*/)];
[label setText:@"Your title"];

// Set a tag on the UIImageView so you can get it later
imageView.tag = 123;

// Put it all together
[button addSubview:label];
[button addSubview:imageView];

Changing an Image in UIImageView at runtime

Once you have an image view, you don't need to create new ones (every time you hit your button).

You could declare these at the top of your class (with the imageView)

let ofImage = UIImage(named: "OF_farm.png")
let opaImage = UIImage(named: "OPA_farm.png")

and in your case statement just reassign either image to your imageView

    switch farmSegment.selectedSegmentIndex {
case 1:
imageView.image = ofImage
// other things
break
default:
imageView.image = opaImage
// other things
}

In viewDidLoad, after adding the imageView to the scrollView you can add auto layout constraints if they are needed. Just be sure to set this first:

 imageView.translatesAutoresizingMaskIntoConstraints = false

One last note: Don't forget to view your work in the simulator along the way using the 7+ and the SE settings (and others) to ensure that you have something that will work for everyone.

How do you create a UIImage View Programmatically - Swift

First you create a UIImage from your image file, then create a UIImageView from that:

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)

Finally you'll need to give imageView a frame and add it your view for it to be visible:

imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(imageView)

How to change an image with a viewWithTag programmatically?

viewWithTag will give UIView in return. But you need ImageView type. So you need to explicitly cast it and assign the image.

if let imgView = vc.view.viewWithTag(8) as? UIImageView {
imgView.image = UIImage(named: "image")
}

how to change uiimage's image on button click in swift?

You don't need to specify either the asset catalog's name or the image file's extension. Try removing those. For example,

myImageView.image = UIImage(named: "tick")


Related Topics



Leave a reply



Submit