-initWithContentsOfFile: for an NSMutableArray

Go To StackoverFlow.com

0

I stored images in an NSMutableArray, and now I'm trying to get them to show up in viewDidLoad. I tried calling initWithContentsOfFile, but that doesn't seem to work. This is how the code looks: imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];

I'm not sure what I should use instead of initWithContentsOfFile to have the saved images load, I'm not even sure if I can save images in a plist through user defaults. I've been researching it for awhile now to no avail. Any help is much appreciated, thanks!

EDIT: Here is additional code:

- (IBAction)grabImage {
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        _popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
        [_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    } 

    else {
        [self presentModalViewController:imgPicker animated:YES];
    }
    [self.imgPicker resignFirstResponder];
}
// Sets the image in the UIImageView
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    if (imageView.image == nil) {
        imageView.image = img;

        [self.array addObject:imageView.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;

    }

    if (imageView2.image == nil) {
        imageView2.image = img;
        NSLog(@"The image is a %@", imageView);
        [self.array addObject:imageView2.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }

    if (imageView3.image == nil) {
        imageView3.image = img;

        [self.array addObject:imageView3.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }
}

- (void)applicationDidEnterBackground:(UIApplication*)application {
    NSLog(@"Image on didenterbackground: %@", imageView);

   [self.array addObject:imageView.image];
    [self.array addObject:imageView2.image];
     [self.array addObject:imageView3.image];


            [self.user setObject:self.array forKey:@"images"];
    [user synchronize];

            }

- (void)viewDidLoad
    {
        self.user = [NSUserDefaults standardUserDefaults];
        NSLog(@"It is %@", self.user);
        self.array = [[self.user objectForKey:@"images"]mutableCopy];
        imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];
        imageView2.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:1]];
        imageView3.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:2]];




        UIApplication *app = [UIApplication sharedApplication];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidEnterBackground:)
                                                     name:UIApplicationDidEnterBackgroundNotification
                                                   object:app];

        backToGalleryButton.hidden = YES;
        tapToDeleteLabel.hidden = YES;
        deleteButton1.hidden = YES;
        [super viewDidLoad];

    }

EDIT: This is how I'm tagging the images and deleting them based upon their tags:

- (IBAction)deleteButtonPressed:(id)sender {
    NSLog(@"Sender is %@", sender);
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
    [deleteAlertView show];

    int imageIndex = ((UIButton *)sender).tag;
    deleteAlertView.tag = imageIndex;
}

- (UIImageView *)viewForTag:(NSInteger)tag {
    UIImageView *found = nil;
    for (UIImageView *view in self.array) {
        if (tag == view.tag) {
            found = view;
            break;
        }
    }
    return found;
}

- (void)alertView: (UIAlertView *) alertView 
clickedButtonAtIndex: (NSInteger) buttonIndex
{


    if (buttonIndex != [alertView cancelButtonIndex]) {
        NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);
        NSLog(@"The tag is %i", alertView.tag);

        UIImageView *view = [self viewForTag:alertView.tag];
        if (view) {
            [self.array removeObject:view];
        }

        NSLog(@"After deleting item, array count  = %d", [array count]);
    NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view);
        ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
    }

    [self.user setObject:self.array forKey:@"images"];
}
2012-04-05 23:39
by Henry F
Do you get a crash? What's the error reported - joerick 2012-04-05 23:43
@joerick No sir, I don't get a crash. But, the images do not save / load correctly when I close the app (in the multitasking bar as well) and relaunch the app. I have a feeling that initWithContentsOfFile is the culprit - Henry F 2012-04-05 23:47
What is actually stored in the array? A path? A URL? If its just storing a UIImage instance, you can't use initWithContentsOfFile.. - Daniel 2012-04-05 23:49
@Daniel It is storing an image. I have the user select a picture from their camera roll, and I store the image they selected in the array. I can post the code if you'd like - Henry F 2012-04-05 23:51
@Joey502 you don't understand your own code? If that is the case, then maybe go read up on some documentation before asking questions here - Richard J. Ross III 2012-04-05 23:52
If you have an image, why not do imageView.image = [self.array objectAtIndex:0] copy], which will create a copy of the stored image - Daniel 2012-04-05 23:53
@Daniel Thanks. I added that code instead, but the images still do not load when the app starts up - Henry F 2012-04-05 23:57
Show the code where you store the image in the array. That would help us to help you - rdelmar 2012-04-06 00:01
@rdelmar I just added the additional code in the OP, thanks - Henry F 2012-04-06 00:03


2

The problem is that you can't store images in a property list, which is what you're trying to do when you save it in the user defaults. You need to use an archiver to convert the image to an NSData object which you can store.

2012-04-06 00:37
by rdelmar
I was doing that previously, but I need to tag the images. NSData doesn't have tag properties that I can check - Henry F 2012-04-06 00:39
How were you tagging the images? I didn't see anything in your code about that - rdelmar 2012-04-06 00:48
I'm tagging them in interface builder (to allow the user to delete the images if they want to). I just updated my OP with the code I use to do that - Henry F 2012-04-06 00:52
Now I'm confused -- it looks like you're tagging the views, not the images themselves. In your for-in loop, you loop through an array of UIImageViews. Where did that come from? I thought array was an array of images. In any case, the problem of tagging is irrelevant to the problem of storing images in a plist -- you can't do that, so you have to archive your array to save it this way - rdelmar 2012-04-06 01:01
Ah thats a bummer. Yeah I'm tagging the views. The way that I had the code set up earlier (Using NSData instead) saved / loaded the images just fine, but then my code to delete them didn't work. Now, my code to delete them works, but saving / loading them doesn't. I'm pretty lost now man lol - Henry F 2012-04-06 01:04
Can't you just get the image from the imageView (that the user wants to delete) and use [array indexOfObject:imageToDelete] to delete it from your array - rdelmar 2012-04-06 01:09
Thanks I'll give that a shot. I'm still very new to programming so I'm not sure how to do that, but I will look into it - Henry F 2012-04-06 01:13


0

It looks like you're not passing a valid image path to the initializer method. Make sure the path is correct, and that it includes the image extension.

Really, though, you shouldn't be calling initWithContentsOfFile: in this case, because UIImageView's image property retains the image when you set it. That will usually lead to a memory leak (unless you're using automatic reference counting). Use one of the static initializers instead, such as imageNamed:, which has the added bonuses of using the system cache and also automatically loading the correct version of the image based on the characteristics of the device (for instance, it will load a higher resolution variant of the image if the device has a retina display).

2012-04-06 00:03
by Bekenn
Ads