简体   繁体   English

在 UIImageView 中显示本地图像

[英]Display local image in UIImageView

How do I display an image in the UIImageView component from the hard drive?如何在硬盘驱动器的 UIImageView 组件中显示图像?

I have a local folder called "images" and would like to access it via a relative path, since I want to package the images with the app.我有一个名为“images”的本地文件夹,想通过相对路径访问它,因为我想用应用程序 package 图像。

To load image from somewhere on your drive use:要从驱动器上的某个位置加载图像,请使用:

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

Or add image to the Xcode project and use:或者将图像添加到 Xcode 项目并使用:

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

After that add image to imageview:之后将图像添加到 imageview:

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

You dont need to give any path as long as the image is in your resources folder.只要图像在您的资源文件夹中,您就不需要提供任何路径。

You can display that image in the imageView using this.您可以使用它在 imageView 中显示该图像。

yourImageView.image = [UIImage imageNamed:@"something.png"];
UIImage *img = [UIImage imageWithContentsOfFile:(the file path)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

check the docs for imageWithContentsOfFile:检查imageWithContentsOfFile 的文档:

What do you mean by images in hard drive.Are you saying that you have kept your images in your bundle in iphone app.If so then you can make array of those images like this....硬盘中的图像是什么意思。您是说您已将图像保存在 iphone 应用程序中的捆绑包中。如果是这样,那么您可以像这样制作这些图像的数组....

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

And then can access your images by this array;然后可以通过这个数组访问你的图像;

Or if you simply want to show a single image from the bundle you can do this way或者,如果您只是想显示捆绑包中的单个图像,您可以这样做

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];

This will help you.这将对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM