ios 在 UIImageView 中显示本地图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6674597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:43:44  来源:igfitidea点击:

Display local image in UIImageView

iosobjective-ciphoneimagesource

提问by mirzahat

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”的本地文件夹,我想通过相对路径访问它,因为我想用应用程序打包图像。

回答by Valerii Pavlov

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

回答by Robin

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

回答by Mridul Kashatria

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

check the docs for imageWithContentsOfFile:

检查imageWithContentsOfFile的文档

回答by Sabby

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.

这会帮助你。