0

在我的应用程序中,用户可以将图像保存到他们的文档目录中。在启动时,我抓取图像,添加边框,然后将其放入 UIImageview 中,如下所示......

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *docDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/ImageOne.jpg", docDirectory];
        UIImage *unborderedImage  = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];

        //image found....add border
        UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
        imageOneView.image = imageWithBorder;

理想情况下,我喜欢在添加边框之前先检查图像是否存在。如果没有,请加载“图像不可用”占位符。像这样的东西:

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *docDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/ImageOne.jpg", docDirectory];
        UIImage *unborderedImage  = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];

NSError * error;

    if (error != nil) {
        //image found....add border
        UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
        imageOneView.image = imageWithBorder;
    } else
        //no image saved
        [imageOneView setImage:[UIImage imageNamed:@"photoNotAvailable.png"]];
    }

当然,这是行不通的。如果找不到“ImageOne.jpg”,我似乎无法弄清楚如何处理。

4

2 回答 2

0

事实证明,我需要在应用程序的其他地方对图像做其他事情。这也取决于用户是否保存了图像。因此,在我的用户可以保存图像的方法中,我发送了一个图像已更改的 NSNotification。然后在我的 MainView 上,我寻找通知并关闭它。

保存时:

[collectionOneUserDefinedDefaults setObject:@"image added" forKey:@"collectionOneImageAdded"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"collectionOneImageChanged" object:self];

然后在我的 MainView 中查找通知:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCollectionOneImage) name:@"collectionOneImageChanged" object:nil];

- (void)updateCollectionOneImage {

    //check if an image was ever saved, if so, replace the noPhotoAvailalble placeholder
    NSUserDefaults *collectionOneUserDefinedDefaults = [NSUserDefaults standardUserDefaults];
    NSString *collectionOneImageTextString = [collectionOneUserDefinedDefaults stringForKey:@"collectionOneImageAdded"];

    if (collectionOneImageTextString == nil || [collectionOneImageTextString isEqualToString:@""]) {
        [collectionOneImage setImage:[UIImage imageNamed:@"photoNotAvailable.png"]];
    }
    else {
        //pull in collection one image from the Documents folder
        NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *docDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/CollectionOneImage.jpg", docDirectory];
        UIImage *unborderedImage  = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
        //image found....add border
        UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
        collectionOneImage.image = imageWithBorder;
    }
}

它完美地工作。它反过来建立在错误处理中。如果用户保存和图像,它会被加载。如果没有,则加载占位符图像。

于 2012-09-27T14:02:20.523 回答
-1

您在这里的错误处理什么也没做,而且被误导了。您需要做的只是检查是否unborderedImage为 nil。

于 2012-09-27T12:45:01.633 回答