2011-04-14 76 views
53

我在項目中添加了一個裝滿圖像的.bundle文件夾。 是否正確是指這個圖像直接寫入類似:?從IOS下載包中加載映像

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

這是訪問和使用這些圖像的最佳方法是什麼?

回答

46

這是正確的,imageNamed:將搜索您的主包。即使您的項目中的圖像位於Project Navigator中的不同組中,它們也會位於您的主包中,並且可以通過名稱直接訪問它們。

+0

如果你的圖片是在主束這隻會工作。 – Tyler 2016-07-05 18:39:04

49

如果還是不行,請嘗試

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

最佳

24

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]; 
+0

將COMBINE_HIDPI_IMAGES設置爲NO對我無效。我嘗試了所有其他建議,但是這樣做......謝謝! – 2013-08-25 01:17:51

5

因爲我必須想出解決辦法的斯威夫特,以爲我還要補充....

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

當我在Supporting FilesTestImage.png我的捆綁包內的組。

2

斯威夫特版本

@ AndrewMackenzie的答案沒有工作我。這是什麼工作

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

我的完整答案是here

2

一個快速的方法來做到這一點,如果你要使用它幾次在相同的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"]; 
4

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

蘋果已經謝天謝地中提供適當的API,用於這iOS 8,imageNamed:inBundle:compatibleWithTraitCollection:

它用於像這樣:

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

或SWIFT:

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

對於斯威夫特3

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

斯威夫特3

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

我不能讓當前的包,所以我這樣做:

Bundle(for: type(of: self))