嗨我的视图之一中有一个名为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
你得到的错误是什么? – 2011-05-23 20:27:35
@ Deepak.I已编辑我的帖子以显示错误 – banditKing 2011-05-23 20:49:55