从iOS的bundle中加载图像

70
我将一个包含一些图像的 .bundle 文件夹添加到了我的项目中。直接使用以下方式引用这些图像是否正确?:
[UIImage imageNamed:@"imageInBundle.png"];

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

13个回答

57

没错,imageNamed:会搜索你的主要包。即使你的项目中有不同组的图像,它们也会在你的主要包中,并且可以通过名称直接访问。


2
只有当您的图像位于主捆绑包中时,此方法才能正常工作。 - Tyler

57

如果那不起作用,请尝试

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

最好的


39

苹果公司自iOS 8版本起,提供了一个适当的API:imageNamed:inBundle:compatibleWithTraitCollection:

它的使用方法如下:

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

或者在Swift中:

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)

27

1) 如果您正在使用bundles,请前往 bundle 目标并将 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];

1
将COMBINE_HIDPI_IMAGES设置为NO对我有用。我尝试了所有其他建议,但这个解决了问题...谢谢! - Brandon Brodjeski

10

Swift 3

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

我无法获取当前包,所以我做了这个:

Bundle(for: type(of: self))

7
你需要按照以下步骤进行操作:
1. 将图像文件添加到你的Xcode项目中: 首先,确保你拥有想要在项目中使用的图像文件。将它们拖放到你的Xcode项目中,并确保它们被添加到正确的目标中。
2. 创建一个资源包: 接下来,你需要创建一个包含图像的资源包。 具体操作如下:在Xcode中,点击"文件" -> "新建" -> "目标"。在模板选择对话框中选择"iOS"或"macOS"。向下滚动并从模板列表中选择"Bundle"。点击"下一步"并给你的资源包命名(例如"ImageResourcesBundle")。
3. 确保将资源包添加到主应用程序目标中,并将图像文件添加到资源包中: 创建资源包后,确保将图像文件添加到资源包的目标中。为此,在Xcode的项目导航器中选择资源包,然后将图像文件拖放到资源包的"复制资源包内容"构建阶段中。
4. 从资源包中加载图像: 最后,你可以使用适当的Bundle方法从资源包中加载图像。
以下是一个示例,说明如何操作:
Swift Code

**

func getImageFromResourceBundle(named imageName: String) -> UIImage? {
    // Replace "ImageResourcesBundle" with the name of your resource bundle.
    guard let bundleURL = Bundle.main.url(forResource: "ImageResourcesBundle", withExtension: "bundle"),
          let resourceBundle = Bundle(url: bundleURL) else {
        return nil
    }
    
    // Load the image from the resource bundle.
    return UIImage(named: imageName, in: resourceBundle, compatibleWith: nil)
}

Objective C 代码

+(UIImage*) getImageFromResourceBundle:(NSString*) imageName
{
    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;
}

你可以调用这个函数从资源包中获取图像。
if let image = getImageFromResourceBundle(named: "imageName") {
    // Do something with the image.
} else {
    // The image couldn't be loaded.
}

6

Swift版本

@AndrewMackenzie的答案对我没用。以下是可行的方法:

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

My full answer is here.


5

由于我之前需要为Swift解决此问题,因此我想添加以下内容...

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

我在我的捆绑包中的Supporting Files组内有一个名为TestImage.png的文件。


5

对于Swift 3

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

你可能需要使用 MyClassName.self 而不是 self - Morgan

2

如果您将在同一视图控制器中多次使用它,则可以通过以下方法快速实现:

在与下面方法相同的类中获取图像:

// 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;
}

或者直接从bundle中调用:
UIImage *myImage = [UIImage imageNamed:@"MyBundle.bundle/folder/to/images/myImage.png"];

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