سؤال

I'm working on a mini game.

In the game, the user can choose the own pictures, and use them in the game.

It's working fine, when I choose the picture, and save them to the app.

But when I load the picture into the game, I get this Error

 [__NSArrayM insertObject:atIndex:]: object cannot be nil

I think it's because the app cannot find the picture. But I cannot see why. :-D

Yes I am a newbie with Xcode :-D

my code look like this

- (void)loadCellImages 
{
    [self unloadCellImages];
    for (int i=1;i<(totalcolors+1);i++) 
    {
        imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];     
        [cellImages addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@-%d",[imgSL loadImage:@"Photo-"],i] ofType:@"png"]]];
    }
}

and I load and save the picture from this:

imageSaveLoad.m

//saving an image
- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName
{
    NSData *imageData = UIImagePNGRepresentation(image);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = paths[0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

    NSLog(@"image saved %@",imageName);
}

//removing an image
- (void)removeImage:(NSString*)fileName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = paths[0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];
    [fileManager removeItemAtPath: fullPath error:NULL];
    NSLog(@"image removed");
}

//loading an image
- (UIImage*)loadImage:(NSString*)imageName
{
   // NSLog(@"load %@",imageName);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = paths[0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
    return [UIImage imageWithContentsOfFile:fullPath];

}
هل كانت مفيدة؟

المحلول

You're saving images to the NSDocumentDirectory but trying to load from your main bundle:

[cellImages addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@-%d",[imgSL loadImage:@"Photo-"],i] ofType:@"png"]]];

You probably just want:

[cellImages addObject:[imgSL loadImage:[NSString stringWithFormat:@"Photo-", i]]];

You use the mainBundle for images (and resources) bundled with your app.

EDIT: To make sure your images are being saved correctly, you should also check the return BOOL of createFileAtPath:contents:attributes:.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top