嗨我的視圖之一中有一個名爲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