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

How can I change the image displayed in a UIImageView programmatically?(如何以编程方式更改 UIImageView 中显示的图像?)

本文介绍了如何以编程方式更改 UIImageView 中显示的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 IBOutlet 到一个 UIImageView,但是当我查看 UIImageView 文档时,我看不到任何关于编程的提示改变它.我是否必须从该 UIImageView 中获取 UIImage 对象?

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. Do I have to fetch an UIImage object from that UIImageView?

推荐答案

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

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.

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

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

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

Once you have an Image you can then set UIImageView:

[imageView setImage:image];

以上行假设 imageView 是您的 IBOutlet.

The line above assumes imageView is your IBOutlet.

就是这样!如果您想变得花哨,可以将图像添加到 UIView,然后添加过渡.

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

附:不包括内存管理.

这篇关于如何以编程方式更改 UIImageView 中显示的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何以编程方式更改 UIImageView 中显示的图像?

基础教程推荐