ios - 从路径获取 UIImage 仅在调试器中单步执行时有效

标签 ios objective-c iphone debugging uiimage

在我的应用程序中,我将图像保存到设备的“文档”文件中,然后再检索它以用作按钮背景。我正在 2 种不同的设备上进行测试,iOS 8 上的 iPhone 5 和 iOS 7 上的 iPhone 4。iPhone 5 工作正常,但我在 iPhone 4 上遇到了一个非常奇怪的问题。

使用以下代码设置 UIImage“imageToUse”仅在调试中单步执行时有效。如果我在此代码块前后放置断点,则图像结果为零。如果我改为逐步执行,则图像会正确设置。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    pathToSave = [paths objectAtIndex:0];
    path = [pathToSave stringByAppendingPathComponent: [NSString stringWithFormat:@"UserIcons/%@", customIcon[i]]]; //customIcon[i] contains "userIcon0.jpg"
    imageToUse = [UIImage imageWithContentsOfFile:path];
    // path contains "/var/mobile/Applications/7E6B8FFA-B56A-48EF-A788-CDB3842F8BB7/Documents/UserIcon‌​s/userIcon0.jpg"

这是一个我以前从未遇到过的非常奇怪的问题。任何想法可能是什么原因?谢谢大家。

更新:这是我按照 matt 的要求保存方法和文件夹检查/创建。

    __block bool exit = NO;
    __block int count = 0;
    while (!exit) {

        // updated with matt's advice
        NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        Path = [NSString stringWithFormat:@"%@/UserSlides/userSlide%i.jpg", docs, count];

        // check to see if file already exists at the path
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:Path];
        // if the path is available, write to disk and then exit
        if (!fileExists) {
            [UIImageJPEGRepresentation(globe.customSlideImage, 1.0) writeToFile:Path atomically:NO];
            BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:Path];
            if (fileExists) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Save Complete"
                                                                message:nil
                                                               delegate:self
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
                [alert show];
                exit = YES;
            }
        }
        else { // otherwise increment the path file name and retry
            count++;
        }
    }

//////////////

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     dataPath = [[paths lastObject] stringByAppendingPathComponent:@"/UserIcons"];
     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 

最佳答案

所以我们有这两行:实际上:

imageToUse = [UIImage imageNamed:path]; 
imageToUse = [UIImage imageWithContentsOfFile:path]; 

现在,这里的问题是我不希望两者都起作用,永远。这是因为他们期待非常不同的东西。

  • imageNamed: 想要一个简单的名称,例如"myFile.jpg"。它会在您的应用程序包中查找文件,而不会在其他地方查找。

  • imageWithContentsOfFile: 想要一个真实的路径名。

    • 如果文件在您的应用程序包中,则只能通过调用 NSBundle.mainBundle()pathForResource:ofType: 开始派生此路径: >(或类似的)。

    • 如果文件在其他地方,您需要使用 NSFileManager 获取包含文件夹并从那里向下钻取。

现在,您在问题中说您正在尝试从 Documents 文件夹中获取文件。好吧,imageNamed: 永远 无法从那里获取文件。我的猜测是,也许一个错误会让您以这种方式获取文件,但这不是您应该依赖的东西。因此,您的第一步应该是完全消除该调用并专注于 imageWithContentsOfFile:,它可以满足您的需求。

编辑:

Path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/UserSlides/userSlide%i.jpg", count]];

哇哦。这不是获取文档目录的方法。因此,第一步是正确使用这些目录。获取文档目录:

NSFileManager* fm = [NSFileManager new];
NSError* err = nil;
NSURL* docsurl =
    [fm URLForDirectory:NSDocumentDirectory
               inDomain:NSUserDomainMask appropriateForURL:nil
                 create:YES error:&err];

或者,如果你想要一个路径字符串:

NSString* docs = [NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

此外,我没有看到您的代码对确保中间 UserSlides 目录存在做任何事情。

关于ios - 从路径获取 UIImage 仅在调试器中单步执行时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003593/

相关文章:

ios - 如何在方法中使用 __weak 来防止 iOS 中的内存泄漏

ios - 如何将 appDelegate.m(Objective-C) 中的内容导入到 Swift 项目中?

objective-c - UIImagePickerController 在 iOS 8 中没有正确呈现

css - 媒体查询是否需要满足所有条件才能工作?

iphone - 如果更新了包含应用内购买产品的应用,会发生什么情况?

iphone - 网站在 iPhone 上显示为左对齐?

ios - 在 iPhone App 中如何检测设备的屏幕分辨率

iOS QuickBlox SDK 按用户 ID 过滤自定义对象

objective-c - 在 Objective-C 中,如何完全替换父类的方法,同时保留父类方法的继承功能?

ios - 无法将类型 '(Void) -> ()' 的值转换为预期的参数类型 '() -> Void'