【问题标题】:Load image from bundle with iOS使用 iOS 从捆绑包中加载图像
【发布时间】:2011-08-05 14:33:42
【问题描述】:

我在我的项目中添加了一个.bundle 文件夹,里面有一些图像。 直接引用这张图片是否正确?:

[UIImage imageNamed:@"imageInBundle.png"];

访问和使用这些图像的最佳方法是什么?

【问题讨论】:

    标签: ios objective-c uiimage


    【解决方案1】:

    如果不行,试试

    [UIImage imageNamed:@"yourbundlefile.bundle/imageInBundle.png"];
    

    最好的

    【讨论】:

      【解决方案2】:

      没错,imageNamed: 将搜索您的主包。项目中的图像,即使它们位于项目导航器中的不同组中,也将位于您的主包中,并且可以通过名称直接访问。

      【讨论】:

      • 这仅在您的图像位于主包中时才有效。
      【解决方案3】:

      幸运的是,Apple 从 iOS 8 开始为此提供了适当的 API,imageNamed:inBundle:compatibleWithTraitCollection:

      它是这样使用的:

      [UIImage               imageNamed:@"image-name"
                               inBundle:[NSBundle bundleForClass:self.class]
          compatibleWithTraitCollection:nil]
      

      或迅速:

      let image = UIImage(named: "image-name",
                          inBundle: NSBundle(forClass: type(of:self)),
                          compatibleWithTraitCollection: nil)
      

      或在 swift 5 中:

      let image = UIImage(named: "image-name",
                          in: Bundle(for: type(of:self)),
                          compatibleWith: nil)
      

      【讨论】:

        【解决方案4】:

        1) 如果您正在使用捆绑包,请转到捆绑包目标并将 COMBINE_HIDPI_IMAGES 设置为 NO。 在另一种情况下,图像将被转换为 tiff 格式。

        2) 试试这个代码:

        NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"YourBundle" withExtension:@"bundle"]];
        NSString *imagePath = [bundle pathForResource:@"imageInBundle" ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        

        【讨论】:

        • 将 COMBINE_HIDPI_IMAGES 设置为 NO 对我有用。我尝试了所有其他建议,但这样做了....谢谢!
        【解决方案5】:

        斯威夫特 3

        let myImage = UIImage(named: "nameOfImage", in: Bundle(for: type(of: self)), compatibleWith: nil)
        

        我无法获取当前捆绑包,所以我这样做:

        Bundle(for: type(of: self))
        

        【讨论】:

          【解决方案6】:

          此方法返回 xcode 项目中任意位置的图像:=>

          +(UIImage*)returnImageFromResourceBundle:(NSString*)nameOfImage
          {
              NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];
              NSString *imageString = [[NSBundle bundleWithPath:bundlePath] pathForResource:nameOfImage ofType:@"png"];
          
              UIImage *retrievedImage = [[UIImage alloc] initWithContentsOfFile: imageString];
              return retrievedImage;
          }
          

          【讨论】:

            【解决方案7】:

            Swift 版本

            @AndrewMackenzie 的回答对我不起作用。这确实有效

            let image = UIImage(named: "imageInBundle.png")
            imageView.image = image
            

            我的完整答案是here

            【讨论】:

              【解决方案8】:

              因为我必须为 Swift 解决这个问题,所以我想我也会添加....

              if let imagePath = NSBundle.mainBundle().pathForImageResource("TestImage.png")     
              {
                  imageView.image = NSImage(contentsOfFile: imagePath)
              }
              

              我的包中 Supporting Files 组中有 TestImage.png

              【讨论】:

                【解决方案9】:

                对于 Swift 3

                let prettyImage = UIImage(named: "prettyImage",
                            in: Bundle(for: self),
                            compatibleWith: nil)
                

                【讨论】:

                • 您可能需要使用MyClassName.self 而不是self
                【解决方案10】:

                如果您要在同一个 viewController 中使用几次,这是一种快速的方法:

                在同一个类的任意位置获取图片,方法如下:

                // this fetches the image from: MyBundle.bundle/folder/to/images/myImage.png
                UIImage *myImage = [self imageFromBundle:@"MyBundle" withPath:@"folder/to/images/myImage.png"];
                

                获取图片的方法:

                - (UIImage *)imageFromBundle:(NSString *)bundleName withPath:(NSString *)imageName
                {
                    NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"];
                    NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
                    NSString *imagePath = [bundle pathForResource:imageName ofType:nil];
                    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
                    return image;
                }
                

                或者直接从捆绑包中调用它:

                UIImage *myImage = [UIImage imageNamed:@"MyBundle.bundle/folder/to/images/myImage.png"];
                

                【讨论】:

                  【解决方案11】:

                  如果您正在构建一个框架,并且您希望 UIImage(named: "image") 的所有实例始终使用您的自定义包,您可以制作如下所示的扩展来覆盖它

                  // Swift 5
                  
                  extension UIImage {
                    convenience init?(named: String) {
                      self.init(named: named, in: <#Your Bundle#>, compatibleWith: nil)
                    }
                  }
                  

                  【讨论】:

                  • 最好不要用扩展覆盖系统方法,因为导入这样做的东西会在你没有意识到的情况下改变你的行为。如果您确实在框架中采用这种方法,最好将其设为 internal 而不是 public
                  • @SamSoffes 你是对的,不知道为什么我把 public 放在扩展中,这是一个错误,谢谢
                  【解决方案12】:

                  这在 Swift 5

                  中对我有用
                  // Empty UIImage array to store the images in it.
                  var icons = [UIImage]()
                  
                  let fileManager = FileManager.default
                  let bundleURL = Bundle.main.bundleURL
                  let assetURL = bundleURL.appendingPathComponent("MyBundle.bundle") // Bundle URL
                  do {
                    let contents = try fileManager.contentsOfDirectory(at: assetURL,
                   includingPropertiesForKeys: [URLResourceKey.nameKey, URLResourceKey.isDirectoryKey],
                   options: .skipsHiddenFiles)
                    for item in contents { // item is the URL of everything in MyBundle imgs or otherwise.
                  
                        let image = UIImage(contentsOfFile: item.path) // Initializing an image
                        icons.append(image!) // Adding the image to the icons array
                    }
                  }
                  catch let error as NSError {
                    print(error)
                  }
                  

                  【讨论】:

                    【解决方案13】:

                    我在尝试解决从 Swift 包加载图像时发现了这个问题,所以如果其他人也在为此苦苦挣扎,这就是我的解决方法:

                    let image = UIImage(named: "imageName", in: Bundle.module, compatibleWith: nil)

                    【讨论】:

                      猜你喜欢
                      • 2023-03-11
                      • 1970-01-01
                      • 2014-11-27
                      • 2021-06-27
                      • 2010-12-15
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多