21

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

我有一个名为“images”的本地文件夹,并且想通过相对路径访问它,因为我想将图像与应用程序打包在一起。

4

4 回答 4

58

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

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

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

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

之后将图像添加到 imageview:

UIImageView * myImageView = [[UIImageView alloc] initWithImage: myImage];
于 2011-07-13T05:59:22.693 回答
9

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

您可以使用它在 imageView 中显示该图像。

yourImageView.image = [UIImage imageNamed:@"something.png"];
于 2011-07-13T05:54:15.957 回答
2
UIImage *img = [UIImage imageWithContentsOfFile:(the file path)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

检查imageWithContentsOfFile 的文档:

于 2011-07-13T05:56:19.637 回答
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];

这将对您有所帮助。

于 2011-07-13T05:59:42.177 回答