iPhone:以编程方式从文档文件夹设置图像
您好,我的视图之一中有一个 UIImageView 出口,名为 promoBanner。我正在尝试编写代码以编程方式设置此插座。基本上,该应用程序在 NSBundle 中附带了一张图像。但我想确保它从文档文件夹中获取图像。我想这样做是因为将来会有不同的“促销”,并且应用程序应该在“promoBanner”图像视图中显示正确的图像。这是我编写的代码,但我不确定为什么它不起作用。有人可以给我一个想法吗:
- (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 raise:format:arguments:] + 70
CoreFoundation 0x3759daf7 +[NSException提高:格式:] + 30
基础 0x351a8653 -[NSFileManager copyItemAtPath:toPath:错误:] + 90
餐厅 0x000068c3 -[AboutViewController viewDidLoad] + 286
UIKit 0x35926e60 -[UIViewController 视图] + 160
Hi I have a UIImageView Outlet in one of my views called promoBanner. I am trying to write code that will set this outlet programmatically. Basically, the app ships with an image in the NSBundle. But what I want to ensure that it fetches the image from the documents folder. I want to do this is because in the future there will be different "promotions" and the app should display the correct image in the "promoBanner" image view. Here is the code that I wrote but Im not sure why its not working. Could someone please give me an idea:
- (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);
}
Thanks a lot
Error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil'
* Call stack at first throw:
CoreFoundation 0x3759dc7b __exceptionPreprocess + 114
libobjc.A.dylib 0x32d9bee8 objc_exception_throw + 40
CoreFoundation 0x3759dac3 +[NSException raise:format:arguments:] + 70
CoreFoundation 0x3759daf7 +[NSException raise:format:] + 30
Foundation 0x351a8653 -[NSFileManager copyItemAtPath:toPath:error:] + 90
Restaurant 0x000068c3 -[AboutViewController viewDidLoad] + 286
UIKit 0x35926e60 -[UIViewController view] + 160
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
返回零。如果该文件不在您的工作区中,请添加它。如果是,请检查以确保您确实已将该映像包含在构建中。在 Xcode 4 中:
is returning nil. If the file is not in your workspace, add it. If it is, check to make sure that you've actually included the image in the build. In Xcode 4:
如果 promoBanner.png 不是我的包中的文件,我会收到错误消息。但是,如果我将文件添加到项目中,代码将编译。
您确定已将 promoBanner.png 添加到您的项目中(通过将其拖到 XCode 中或使用“文件 -> 添加文件到”)
I can get your error if promoBanner.png is not a file in my bundle. However, if I add the file to the project, the code will compile.
Are you sure you have added promoBanner.png to your project (either by dragging it into XCode or by using "File -> add Files to"