我将一个包含一些图像的
.bundle
文件夹添加到了我的项目中。直接使用以下方式引用这些图像是否正确?:[UIImage imageNamed:@"imageInBundle.png"];
哪种方法是访问和使用这些图像的最佳方法?
.bundle
文件夹添加到了我的项目中。直接使用以下方式引用这些图像是否正确?:[UIImage imageNamed:@"imageInBundle.png"];
哪种方法是访问和使用这些图像的最佳方法?
没错,imageNamed:
会搜索你的主要包。即使你的项目中有不同组的图像,它们也会在你的主要包中,并且可以通过名称直接访问。
如果那不起作用,请尝试
[UIImage imageNamed:@"yourbundlefile.bundle/imageInBundle.png"];
最好的
苹果公司自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)
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];
Swift 3
let myImage = UIImage(named: "nameOfImage", in: Bundle(for: type(of: self)), compatibleWith: nil)
我无法获取当前包,所以我做了这个:
Bundle(for: type(of: self))
**
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.
}
Swift版本
@AndrewMackenzie的答案对我没用。以下是可行的方法:
let image = UIImage(named: "imageInBundle.png")
imageView.image = image
My full answer is here.
由于我之前需要为Swift解决此问题,因此我想添加以下内容...
if let imagePath = NSBundle.mainBundle().pathForImageResource("TestImage.png")
{
imageView.image = NSImage(contentsOfFile: imagePath)
}
我在我的捆绑包中的Supporting Files
组内有一个名为TestImage.png
的文件。
对于Swift 3
let prettyImage = UIImage(named: "prettyImage",
in: Bundle(for: self),
compatibleWith: nil)
MyClassName.self
而不是 self
。 - Morgan如果您将在同一视图控制器中多次使用它,则可以通过以下方法快速实现:
在与下面方法相同的类中获取图像:
// 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"];