iPhone:以编程方式从文档文件夹设置图像

发布于 2024-11-09 06:43:01 字数 2108 浏览 2 评论 0原文

您好,我的视图之一中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-11-16 06:43:01
[[NSBundle mainBundle] pathForResource:@"promoBanner" ofType:@"png"];

返回零。如果该文件不在您的工作区中,请添加它。如果是,请检查以确保您确实已将该映像包含在构建中。在 Xcode 4 中:

  • 在文件浏览器中选择根项目项。
  • 从“目标”列表中选择适当的目标 选择
  • “构建阶段”选项卡
  • 展开“复制捆绑资源”项并查找您的图像文件。
[[NSBundle mainBundle] pathForResource:@"promoBanner" ofType:@"png"];

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:

  • Select the root project item in the file browser.
  • Choose the appropriate target from the "Targets" list
  • Select the "Build Phases" tab
  • Expand the "Copy Bundle Resources" item and look for your image file.
三生殊途 2024-11-16 06:43:01

如果 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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文