2011-05-23 115 views
0

嗨我的視圖之一中有一個名爲promoBanner的UIImageView插座。我試圖編寫代碼,以編程方式設置此插座。基本上,該應用程序與NSBundle中的圖像一起發貨。但我想確保它從文檔文件夾中提取圖像。我想這樣做是因爲將來會有不同的「促銷」,應用應該在「promoBanner」圖片視圖中顯示正確的圖片。這是我寫的代碼,但我不知道爲什麼它不工作。可能有人請給我一個想法:iPhone:從「文檔」文件夾以編程方式設置圖像

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //========================================================================== 
    NSError *error; 
    NSString *errorDesc = nil; 
    NSPropertyListFormat format; 
    NSData *promoImg; 
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *promoImgDocPath = [rootPath stringByAppendingPathComponent:@"promoBanner.png"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath:promoImgDocPath]) 
     { 
     NSString *plistBundlePath = [[NSBundle mainBundle] pathForResource:@"promoBanner" ofType:@"png"]; 
     [fileManager copyItemAtPath:plistBundlePath toPath:promoImgDocPath error:&error]; 
     if(![fileManager fileExistsAtPath:promoImgDocPath]) 
      { 
      NSLog(@"The copy function did not work, file was not copied from bundle to documents folder"); 
      } 
     } 

    self.promoBanner.image =[UIImage imageWithContentsOfFile:promoImgDocPath]; 

    if (!self.promoBanner.image) 
     { 
     NSLog(@"Error setting image: %@, format: %d", errorDesc, format); 
     } 

非常感謝 錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil' 

*調用堆棧在第一擲:

的CoreFoundation 0x3759dc7b __exceptionPreprocess + 114

libobjc.A.dylib 0x32d9bee8 objc_exception_throw + 40

的CoreFoundation 0x3759dac3 + [NSException提高:格式:參數:] + 70

的CoreFoundation 0x3759daf7 + [NSException提高:格式:] + 30

基金會0x351a8653 - [的NSFileManager copyItemAtPath:toPath:錯誤:] + 90

餐廳0x000068c3 - [AboutViewController viewDidLoad中] + 286

的UIKit 0x35926e60 - [UIViewController的視圖] + 160

+0

你得到的錯誤是什麼? – 2011-05-23 20:27:35

+0

@ Deepak.I已編輯我的帖子以顯示錯誤 – banditKing 2011-05-23 20:49:55

回答

0
[[NSBundle mainBundle] pathForResource:@"promoBanner" ofType:@"png"]; 

將返回零如果文件是沒有的。在您的工作空間,添加它如果是,請檢查以確保您實際上已經包含在構建圖像在Xcode中4:

  • 選擇在文件瀏覽器根項目項目
  • 。從「目標」列表中選擇適當的目標
  • 選擇「Build Phases」選項卡
  • 展開「Copy Bundle Resources」項目並查找圖像文件。
+0

謝謝。它現在有效。 – banditKing 2011-05-23 21:14:31

0

如果promoBanner.png不是我的包中的文件,我可以得到您的錯誤。但是,如果我將該文件添加到項目中,代碼將被編譯。

你確定你已經添加promoBanner.png到您的項目(通過將它拖動到XCode中或使用「文件 - >將文件添加到」

相關問題