如何将图像设置为UIImage

33

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

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

我的UIImage对象在.h文件中声明并在init方法中分配,我需要在viewDidLoad方法中设置图像。有什么想法吗?


3
UIImage *img = [UIImage imageNamed:@"任何图片名称"]; - Warif Akhand Rishi
1
你可以在接口中(也就是所谓的.h文件)声明某些内容,但不能在那里创建它。 - vikingosegundo
12个回答

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

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

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

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

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


对象在 .h 文件中创建并在 init 方法中分配,我需要在 viewDidLoad 方法中设置图像。 - Shamsiddin Saidov
1
创建一个属性并将其分配给它。 - vikingosegundo
不,但如果您想了解有关属性的信息,您应该提出一个新问题,而不是在有关如何使用图像文件创建UIImage的答案的评论中处理它。 - vikingosegundo

11
创建一个 UIImageView 并将 UIImage 添加到其中:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image Name"]] ;

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

[self.view addSubView: imageView];

2

只需按照以下步骤操作

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

1

这一行有一个错误:

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

应该是这样的:

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

3
这是错误的,因为UIImage没有任何名为setImage:的方法。 - BHASKAR
这段代码正确,但是还有一个错误需要解决,Jeremy 先生帮忙解决了。非常感谢您分享您的知识。 - Ramani Hitesh

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

1

可能是:

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

当您想要更改图像时:

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

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


1
这就是所谓的内存泄漏 :) - Shamsiddin Saidov
好的,但在重新分配新图像之前,您创建了一个[img released]。我没有放在这里,因为它是Objective-C的基础... - Vincent Saluzzo
即使在非ARC环境下,-released也会产生选择器未找到的错误。 - vikingosegundo

0

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

  1. UIImage *imageObject = [UIImage imageNamed:@"imageFileName"]; 但是,通过这种方式初始化UIImage对象时,即使您将其设置为nil,图像仍然始终存储在缓存内存中。

最好遵循以下方法:

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

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

UIImage *imageObject = [UIImage imageWithContentsOfFile:imageFilePath];

0

你不能分配UIImage,这是不可能的。 使用UIImageView分配的正确代码:

UIImageView *_image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"something.png"]] ;

绘制图像:

CGContextDrawImage(context, rect, _image.image.CGImage);

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

这将节省大量内存并加快应用程序速度。


0

首先声明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];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接