ios 如何以编程方式更改 UIImageView 中显示的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/706240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:46:50  来源:igfitidea点击:

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

iosuiimageview

提问by Thanks

I have an IBOutletto a UIImageView, but when I look at the UIImageViewdoc, I can't see any hints about programmatically changing it. Do I have to fetch an UIImageobject from that UIImageView?

我有一个IBOutletto a UIImageView,但是当我查看UIImageView文档时,我看不到任何有关以编程方式更改它的提示。我必须从中获取一个UIImage对象UIImageView吗?

回答by Jordan

If you have an IBOutlet to a UIImageView already, then all you have to do is grab an image and call setImage on the receiver (UIImageView). Two examples of grabbing an image are below. One from the Web, and one you add to your Resources folder in Xcode.

如果你已经有一个 UIImageView 的 IBOutlet,那么你所要做的就是抓取一个图像并在接收器 (UIImageView) 上调用 setImage。下面是抓取图像的两个示例。一个来自 Web,另一个添加到 Xcode 的 Resources 文件夹中。

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];

or

或者

UIImage *image = [UIImage imageNamed: @"cell.png"];

Once you have an Image you can then set UIImageView:

一旦你有了一个图像,你就可以设置 UIImageView:

[imageView setImage:image];

The line above assumes imageView is your IBOutlet.

上面的行假设 imageView 是您的 IBOutlet。

That's it! If you want to get fancy you can add the image to an UIView and then add transitions.

就是这样!如果你想变得有趣,你可以将图像添加到 UIView,然后添加过渡。

P.S. Memory management not included.

PS 不包括内存管理。

回答by samkass

Note that the NIB file doesn't wire up all the IBOutlets until the view has been added to the scene. If you're wiring things up manually (which you might be doing if things are in separate NIBs) this is important to keep in mind.

请注意,在将视图添加到场景之前,NIB 文件不会连接所有 IBOutlets。如果您手动连接东西(如果事情在单独的 NIB 中,您可能会这样做),记住这一点很重要。

So if my test view controller has an "imageView" wired by a nib, this probably won't work:

所以如果我的测试视图控制器有一个由笔尖连接的“imageView”,这可能不起作用:

  testCardViewController.imageView.image = [UIImage imageNamed:@"EmptyCard.png"];
  [self.view addSubview:testCardViewController.view];

But this will:

但这将:

  [self.view addSubview:testCardViewController.view];
  testCardViewController.imageView.image = [UIImage imageNamed:@"EmptyCard.png"];

回答by Gilall

This worked for me

这对我有用

[ImageViewName setImage:[UIImage imageNamed: @"ImageName.png"]];

Make sure that the ImageView is declared properly in the .h file and is linked with the IB element.

确保 ImageView 在 .h 文件中正确声明并与 IB 元素链接。

回答by Lukas Wiklund

imageView.image = [UIImage imageNamed:@"myImage.png"];

回答by swiecki

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.

为了那些可能在谷歌上搜索以试图解决他们的问题的人的目的,记住在你的头文件中正确声明属性并在你的实现文件中合成 UIImageView ......如果没有,以编程方式设置图像将很困难getter 和 setter 方法。

#import <UIKit/UIKit.h>

@interface YOURCONTROLLERNAME : UIViewController {
    IBOutlet UIImageView *imageToDisplay;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageToDisplay;

@end

and then in your .m :

然后在你的 .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]];

回答by King-Wizard

Example in Swift:

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")!)!)
    }

}

回答by Adi

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

按照 Jordan 的建议(实际上应该有效),尝试将 UIImageView 设置为可见:

 [imageView setHidden: NO];

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

还有 - 不要忘记将它附加到主 UIView:

[mainView addSubview: imageView];

and to bring to the front:

并将:

[mainView bringSubviewToFront: imageView];

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

希望结合所有这些步骤可以帮助您解开谜团。

回答by Okku

My problem was that I tried to change the image in an other thread. I did like this:

我的问题是我试图在另一个线程中更改图像。我是这样的:

- (void)changeImage {
    backgroundImage.image = [UIImage imageNamed:@"img.png"];
}

Call with:

致电:

[self performSelectorOnMainThread : @selector(changeImage) withObject:nil waitUntilDone:NO];

回答by Rajneesh071

If you want to set image to UIImageViewprogrammatically then Dont Forget to add UIImageViewas SubViewto the main View.

如果你想设置图像UIImageView编程,然后不要忘记添加UIImageView子视图到主视图。

And also dont forgot to set ImageView Frame.

并且不要忘记设置ImageView Frame

here is the code

这是代码

UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

myImage.image = [UIImage imageNamed:@"myImage.png"];

[self.view addSubview:myImage];

回答by Miroslav Hrivik

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

sizeToFit()如果您然后使用 UIImageView 的大小来设置 UIScrollView contentSize 和/或计算缩放比例,请不要忘记在更改图像后调用

let image = UIImage(named: "testImage")
imageView.image = image
imageView.sizeToFit()
scrollView.contentSize = imageView.bounds.size