2011-05-23 104 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中或使用“文件 - >将文件添加到”

相关问题