使用 iOS 从捆绑包中加载图像

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

Load image from bundle with iOS

iosobjective-cuiimage

提问by MatterGoal

I added to my project a .bundlefolder filled with some images. Is it correct refer to this image directly writing something like ?:

我在我的项目中添加了一个.bundle包含一些图像的文件夹。直接参考这个图像是否正确写一些类似的东西?:

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

Which is the best method to access and use these images ?

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

采纳答案by Joe

That is correct, imageNamed:will search your main bundle. Images in your project, even if they are in different groups in your Project Navigator will be in your main bundle and can be accessed directly by name.

这是正确的,imageNamed:将搜索您的主要捆绑包。项目中的图像,即使它们在项目导航器中的不同组中,也将在您的主包中,并且可以通过名称直接访问。

回答by Ignacio Pascual

If that does not work, try

如果这不起作用,请尝试

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

Best

最好的事物

回答by user1794139

1) If you are working with bundles go to bundle target and set COMBINE_HIDPI_IMAGES to NO. In another case the image will be converted to tiff format.

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

2) try this code:

2)试试这个代码:

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

回答by JosephH

Apple has thankfully provided a proper API for this from iOS 8 onwards, imageNamed:inBundle:compatibleWithTraitCollection:.

苹果已经谢天谢地提供一个适当的API,这从iOS版8日起,imageNamed:inBundle:compatibleWithTraitCollection:

It's used like so:

它是这样使用的:

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

or in swift:

或迅速:

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

or in swift 5:

或在 swift 5:

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

回答by oscarr

Swift 3

斯威夫特 3

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

I can't get current bundle so i do this:

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

Bundle(for: type(of: self))

回答by Mohd. Asif

This Method returns image anywhere in the xcode project:=>

此方法返回 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;
}

回答by Pavle Mijatovic

For Swift 3

对于 Swift 3

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

回答by Andrew Mackenzie

Since I had to figure this out for Swift, thought I would add also....

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

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

Where I have TestImage.pngin the Supporting Filesgroup inside my bundle.

TestImage.pngSupporting Files捆绑包中的组中的位置。

回答by Suragch

Swift version

迅捷版

@AndrewMackenzie's answer didn't work me. This is what did work

@AndrewMackenzie 的回答对我不起作用。这是什么工作

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

My full answer is here.

我的完整答案在这里

回答by emotality

A quick way to do it if you are going to use it a couple of times in the same viewController:

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

Get the image anywhere in the same class as the method below:

在与以下方法相同的类中的任何位置获取图像:

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

Method that fetches the image:

获取图像的方法:

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

Or simply call it straight out of the bundle:

或者直接从包中调用它:

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