How can I change the image displayed in a UIImageView programmatically?

后端 未结 15 635
孤街浪徒
孤街浪徒 2020-12-07 08:42

I have an IBOutlet to a UIImageView, but when I look at the UIImageView doc, I can\'t see any hints about programmatically changing it

相关标签:
15条回答
  • 2020-12-07 09:03

    Example in Swift:

    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet var myUIImageView: UIImageView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        @IBAction func myAction(sender: UIButton) {
            let newImg: UIImage? = UIImage(named: "profile-picture-name")
            self.myUIImageView.image = newImg
        }
    
        @IBAction func myAction2(sender: UIButton) {
            self.myUIImageView.image = nil
            self.myUIImageView.image = UIImage(data: NSData(contentsOfURL: NSURL(string: "http://url/image.png")!)!)
        }
    
    }
    
    0 讨论(0)
  • 2020-12-07 09:03

    Don't forget to call sizeToFit() after you change image if you then use size of UIImageView to set UIScrollView contentSize and/or compute zoom scale

    let image = UIImage(named: "testImage")
    imageView.image = image
    imageView.sizeToFit()
    scrollView.contentSize = imageView.bounds.size
    
    0 讨论(0)
  • 2020-12-07 09:03

    Working with Swift 5 (XCode 10.3) it's just

    yourImageView.image = UIImage(named: "nameOfTheImage")
    
    0 讨论(0)
  • 2020-12-07 09:08

    Following Jordan's advice (which should work actually), try to set the UIImageView to be visible:

     [imageView setHidden: NO];
    

    and also - don't forget to attach it to the main UIView:

    [mainView addSubview: imageView];
    

    and to bring to the front:

    [mainView bringSubviewToFront: imageView];
    

    Hope combining all these steps will help you solve the mystery.

    0 讨论(0)
  • 2020-12-07 09:08

    This question already had a lot of answers. Unfortunately none worked for me. So for the sake of completenes I add what helped me:

    I had multiple images with the same name - so I ordered them in sub folders. And I had the full path to the image file I wanted to show. With a full path imageNamed: (as used in all solutions above) did not work and was the wrong method.

    Instead I now use imageWithContentsOfFile: like so:

    self.myUIImage.image = [UIImage imageWithContentsOfFile:_currentWord.imageFileName];
    

    Don't know, if anyone reads that far?

    If so and this one helped you: please vote up. ;-)

    0 讨论(0)
  • 2020-12-07 09:11

    For the purpose of people who may be googling this to try to solve their problem, remember to properly declare the property in your header file and to synthesize the UIImageView in your implementation file... It'll be tough to set the image programmatically without getter and setter methods.

    #import <UIKit/UIKit.h>
    
    @interface YOURCONTROLLERNAME : UIViewController {
        IBOutlet UIImageView *imageToDisplay;
    }
    
    @property (nonatomic, retain) IBOutlet UIImageView *imageToDisplay;
    
    @end
    

    and then in your .m :

    @implementation YOURCONTROLLERNAME
    
    @synthesize imageToDisplay;
    //etc, rest of code goes here
    

    From there you should be fine using something like the following to set your image.

    [YOURCONTROLLER.imageToDisplay setImage:[UIImage imageNamed:value]];
    
    0 讨论(0)
提交回复
热议问题