How can I change the image displayed in a UIImageView programmatically?(如何以编程方式更改 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 中显示的图像?
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
