在UIImageView中显示本地图像

时间:2011-07-13 05:51:15

标签: ios objective-c iphone imagesource

如何在硬盘驱动器中显示UIImageView组件中的图像?

我有一个名为“images”的本地文件夹,想通过亲戚访问它 路径,因为我想用应用程序打包图像。

4 个答案:

答案 0 :(得分:56)

要从驱动器上的某处加载图像,请使用:

UIImage * myImage = [UIImage imageWithContentsOfFile: @"../images/1.png"];

或者将图像添加到Xcode项目并使用:

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

之后将图像添加到imageview:

UIImageView * myImageView = [[UIImageView alloc] initWithImage: myImage];

答案 1 :(得分:10)

只要图像在资源文件夹中,您就不需要提供任何路径。

您可以使用此功能将该图像显示到imageView中。

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

答案 2 :(得分:2)

UIImage *img = [UIImage imageWithContentsOfFile:(the file path)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

查看imageWithContentsOfFile:

的文档

答案 3 :(得分:0)

硬盘驱动器中的图像是什么意思。你说的是你已经将你的图像保存在iphone应用程序中了。如果是这样你可以制作这样的图像数组....

NSMutableArray *imageArray;
imageArray=[[NSMutableArray alloc]initWithObjects:@"001.jpg",@"002.jpg",@"003.jpg",@"004.jpg",nil];

然后可以通过此阵列访问您的图像;

或者,如果您只是想从捆绑中显示单个图像,则可以这样做

UIImageView *creditCardImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 16, 302, 77)];
    creditCardImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"CreditCard_2RowTable" ofType:@"png"]];
    [self.view addSubview:creditCardImageView];

这会对你有帮助。