【问题标题】:How to set image to UIImage如何将图像设置为 UIImage
【发布时间】:2012-06-11 11:45:09
【问题描述】:

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

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

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

【问题讨论】:

  • UIImage *img = [UIImage imageNamed@"anyImageName"];
  • 你可以在接口(你称之为 .h 文件)中声明一些东西,但不能在那里创建它。

标签: objective-c ios cocoa-touch


【解决方案1】:
UIImage *img = [UIImage imageNamed:@"anyImageName"];

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

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

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

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

【讨论】:

  • 对象在.h文件中创建并在init方法中分配,我需要在viewDidLoad方法中设置图像。
  • 所以创建一个属性并将其分配给它
  • 不,但是如果您想了解属性,您应该提出一个新问题,而不是尝试在 cmets 中处理它以回答有关如何使用图像文件创建 UIImage 的问题.
【解决方案2】:

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

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

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

[self.view addSubView: imageView];

【讨论】:

    【解决方案3】:

    请按此操作

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

    【讨论】:

      【解决方案4】:

      这一行有错误:

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

      应该是:

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

      【讨论】:

      • 错了。因为 UIImage 没有任何方法,如 setImage: .
      • 在此代码中正确,但一个错误也解决了 Mr.jeremy .. 谢谢先生分享您的知识
      【解决方案5】:

      可能是:

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

      当你想改变图像时:

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

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

      【讨论】:

      • 这是调用内存泄漏:)
      • 是的,但是在重新分配新图像之前,您制作了一个 [img 发布]。我不放在这里,因为它是Objective-C的基础......
      • 即使在非 ARC 中 -released 也会产生选择器未找到错误。
      【解决方案6】:
      UIImage *img = [UIImage imageNamed@"aImageName"];
      

      【讨论】:

        【解决方案7】:

        就像 vikingosgundo 所说,但请记住,如果您使用 [UIImage imageNamed:image],则图像会被缓存并占用内存。所以除非你打算在很多地方使用同一张图片,否则你应该用imageWithContentsOfFile:imageWithData:加载图片

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

        【讨论】:

          【解决方案8】:

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

          【讨论】:

            【解决方案9】:

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

            1. UIImage *imageObject = [UIImage imageNamed:@"imageFileName"]; 但是通过像这样初始化UIImage对象,图像总是存储在缓存中,即使你把它做成nil就像imageObject=nil;一样

            最好遵循这个:

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

            【讨论】:

              【解决方案10】:

              只需改变这一行

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

              下面一行

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

              【讨论】:

                【解决方案11】:

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

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

                画图:

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

                【讨论】:

                  【解决方案12】:

                  试试这个代码 100% 工作....

                  UIImageView * imageview = [[UIImageView alloc] initWithFrame:CGRectMake(20,100, 80, 80)];
                  imageview.image = [UIImage imageNamed:@"myimage.jpg"];
                  [self.view  addSubview:imageview];
                  

                  【讨论】:

                    猜你喜欢
                    • 2019-02-15
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-10-19
                    • 1970-01-01
                    • 1970-01-01
                    • 2016-01-21
                    • 2013-02-02
                    相关资源
                    最近更新 更多