iphone - 以编程方式将 UIImage 添加到 UIImageView

标签 iphone objective-c

在 .h 文件中我声明了这一点;

IBOutlet UIImageView *image;

在.m文件中;

UIImage *image1 = [UIImage imageName:@"http://image.com/image.jpg"];
image = [[UIImageView alloc] initWithImage:image1];
image.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];

我从界面生成器连接了 UIImageView。但我需要仅通过代码来完成此操作(不使用界面生成器)。有人可以帮我修改代码,以便我只能通过代码来完成此操作吗?

最佳答案

我认为您在 uiimageview 中显示远程图像时遇到一些问题,所以我认为您应该这样做。

NSData *receivedData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://image.com/image.jpg"]] retain];
UIImage *image = [[UIImage alloc] initWithData:receivedData] ;

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];

[image release];
[imageView release];

关于iphone - 以编程方式将 UIImage 添加到 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8523450/

相关文章:

iphone - 从通知中心进入前台时显示 IOS 启动画面

ios - 在不使用来自 iOS 应用程序的任何特定 url 的情况下启动 Safari 应用程序

iphone - NSManagedObjectContext 没有正确刷新

ios - KIF 测试包 XCTest.dylib 在使用 Swift 时未加载

ios - xCode 4项目模板中的可选框架

iphone - 使用 DropBox SDK 的 iPhone 应用程序未调用 handOpenURL

ios - 使用 TesseractOCRiOS 时出错

ios - Action 连接和自动调整大小问题

iphone - 在 NSString 中查找特定字符的位置

objective-c - 释放所有缓存数据