ios 如何将图像设置为 UIImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10931510/
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 18:40:16  来源:igfitidea点击:

How to set image to UIImage

objective-cioscocoa-touch

提问by Shamsiddin

How can I set image to UIImageobject. For example:

如何将图像设置为UIImage对象。例如:

UIImage *img = [[UIImage alloc] init];
[img setImage:[UIImage imageNamed:@"anyImageName"]];

My UIImageobject is declared in .hfile and allocated in initmethod, I need to set image in viewDidLoadmethod. Any ideas?

我的UIImage目标是在申报.h文件和分配init方法,我需要设置图像viewDidLoad的方法。有任何想法吗?

回答by vikingosegundo

UIImage *img = [UIImage imageNamed:@"anyImageName"];

imageNamed:
Returns the image object associated with the specified filename.

    + (UIImage *)imageNamed:(NSString *)name

Parameters
name
The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application's main bundle.
Return Value
The image object for the specified file, or nil if the method could not find the specified image.

Discussion
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

imageNamed:
返回与指定文件名关联的图像对象。

    + (UIImage *)imageNamed:(NSString *)name

参数
name
文件的名称。如果这是第一次加载图像,该方法会在应用程序的主包中查找具有指定名称的图像。
返回值
指定文件的图像对象,如果该方法找不到指定的图像,则为 nil。

讨论
该方法在系统缓存中查找具有指定名称的图像对象,如果存在则返回该对象。如果缓存中还没有匹配的图像对象,则此方法从指定文件加载图像数据,将其缓存,然后返回结果对象。

回答by Shailesh

Create a UIImageView and add UIImage to it:

创建一个 UIImageView 并将 UIImage 添加到它:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image Name"]] ;

Then add it to your view:

然后将其添加到您的视图中:

[self.view addSubView: imageView];

回答by Dixit Akabari

Just Follow This

只关注这个

UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
[imgview setImage:[UIImage imageNamed:@"YourImageName"]];
[imgview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imgview];

回答by Jeremy1026

There is an error on this line:

这一行有一个错误:

[img setImage:[UIImage imageNamed@"anyImageName"]]; 

It should be:

它应该是:

[img setImage:[UIImage imageNamed:@"anyImageName"]]; 

回答by Maulik

UIImage *img = [UIImage imageNamed@"aImageName"];

回答by Vincent Saluzzo

may be:

或许:

UIImage *img = [[UIImage alloc] init];

and when you want to change the image:

当您想更改图像时:

img = [UIImage imageNamed:@"nameOfPng.png"];

but the object wasn't in the same place in the memory, but if you use the pointer, the same pointer will be point to the last image loaded.

但是对象不在内存中的同一位置,但是如果您使用指针,则相同的指针将指向加载的最后一个图像。

回答by Trausti Thor

Like vikingosgundo said, but keep in mind that if you use [UIImage imageNamed:image]then the image is cached and eats away memory. So unless you plan on using the same image in many places, you should load the image, with imageWithContentsOfFile:and imageWithData:

就像 vikingosgundo 所说,但请记住,如果您使用,[UIImage imageNamed:image]则图像会被缓存并占用内存。因此,除非您计划在许多地方使用相同的图像,否则您应该加载图像,使用imageWithContentsOfFile:imageWithData:

This will save you significant memory and speeds up your app.

这将为您节省大量内存并加速您的应用程序。

回答by Ankit Jain

First declare UIImageView and give it frame

首先声明 UIImageView 并给它框架

UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake( 10.0f, 15.0f, 40.0f,40.0f )];
        [imageView setBackgroundColor: [UIColor clearColor]];
        [imageView setImage:[UIImage imageNamed:@"comments_profile_image.png"]];
        [self.view addSubview: imageView];

回答by Satish Azad

You can set an Image in UIImage object eiter of the following way:

您可以通过以下方式在 UIImage 对象中设置图像:

  1. UIImage *imageObject = [UIImage imageNamed:@"imageFileName"];but by initializing UIImageObject like this, the image is always stored in cache memory even you make it nillike imageObject=nil;
  1. UIImage *imageObject = [UIImage imageNamed:@"imageFileName"];但是通过UIImage像这样初始化对象,图像总是存储在缓存中,即使你nil像这样imageObject=nil;

Its better to follow this:

最好遵循以下步骤:

  1. NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"imageFilename" ofType:@"imageFile_Extension"];

    UIImage *imageObject = [UIImage imageWithContentsOfFile:imageFilePath];
    
  1. NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"imageFilename" ofType:@"imageFile_Extension"];

    UIImage *imageObject = [UIImage imageWithContentsOfFile:imageFilePath];
    

EXAMPLE: NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"png"];

例子: NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"png"];

UIImage *imageObject = [UIImage imageWithContentsOfFile:imageFilePath];

回答by BHASKAR

just change this line

只需改变这一行

[img setImage:[UIImage imageNamed:@"anyImageName"]];

with following line

与以下行

img = [UIImage imageNamed:@"anyImageName"];