0

So, the app that I'm writing allows the user to upload a photo from their camera roll, and it displays in a UIImageView. I have a "Save" button, that allows the images to be saved when pressed. I also have an "Edit" button, when tapped, it allows the user to tap on the photo and it will be deleted. This is where I'm having issues.

On test runs when I add in three images, hit the save button, then delete them, they delete perfectly. I can completely close and relaunch the app and the images are no longer there. But, if I add in three images, hit the save button then close and relaunch the app, then try to delete the photos, it does not work at all. The images disappear from the screen, but load again on relaunch. Very strange. I'm totally new to Objective-C, I'm surprised that I even made it this far, so I'm having issues figuring out how to delete it from the array.

I have NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]); in ym code, and when I delete it before closing the app, it displays that the array count has objects in it. When I run it from Xcode again and try to delete a picture thats already been saved, it indicates that the array count = 0. The objects are not in the array on relaunch. So, the issue is that the objects in the array aren't being saved properly. I have no idea why not though, I thought I was saving them correctly. This is how I add them into the array:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    if (imageView.image == nil) {
        imageView.image = img;

        [self.array addObject:imageView];

        [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];

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

        return;
    }
   ...
     ...
-(IBAction)saveButtonPressed:(id)sender {
    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];

    NSLog(@"Image view array before saving = %@", self.array);

    for (UIImageView *imageViewToSave in self.array) {

        NSInteger tag = imageViewToSave.tag;
        UIImage *image = imageViewToSave.image;
        NSString *imageName = [NSString stringWithFormat:@"Image%i.png",tag];

        NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];

        NSLog(@"Saving %@ to %@", image, imagePath);

        [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:NO];
    }
    NSLog(@"Save Button Pressed");
}

I thought that this would save the objects that were placed into the array, but apparently not. I have no idea how else to go about doing this.

And just for reference, I threw my .h and .m files in a wiki. Here is my entire .h file: github.com/Joey502/Test-Thing/wiki/.h-Class-File And here is my entire .m file: github.com/Joey502/Test-Thing/wiki/.m-Class-File

Henry F
  • 4,960
  • 11
  • 55
  • 98
  • where you are initializing your image array and have you debugged that after relaunching that array contains objects? – rishi Apr 23 '12 at 17:16
  • @RIP I have not confirmed that the array contains objects on relaunch, I added in NSLogs throughout my code and did confirm that the array contains the objects before closing it. But, after I close the app completely I can't use Xcode's debug window anymore which makes this kind of difficult. In my .m file that I posted above, it appears that I'm adding the objects into the array in my imagePickerController method, not sure if I'm doing it right though. Could that cause this issue? – Henry F Apr 23 '12 at 17:20
  • 1
    not able to open .m files on github, also try once switching off-on application from Xcode itself, that will enable you to see exactly at what point these is issue. – rishi Apr 23 '12 at 17:26
  • Thanks a lot. I have `NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);` above, and when I delete it before closing the app, it displays that the array count has objects in it. When I run it from Xcode again and try to delete a picture thats already been saved, it indicates that the array count = 0. So you were right, the objects are not in the array on relaunch. So, the issue is that the objects in the array aren't being saved properly. Thanks for helping me narrow that down! I have no idea how to fix this though lol. – Henry F Apr 23 '12 at 17:44
  • I just updated my OP with my code relevant to the issue, I posted how I'm adding / saving the objects in the array. – Henry F Apr 23 '12 at 17:48

1 Answers1

2

The problem is in your viewDidLoad method, there you have initialised self.array but when loading the images into your UIImageViews you have forgotten to add them to the array, which is why the delete function isn't working. Your delete function is trying to delete an object from an array that is empty.

Inside your if statements you need to add the UIImageView to self.array (Note: you'll need to change self.array to a NSMutableArray for this particular method to work)

if (!imageView.image) {
    imageView.image = loadedImage;
    [self.array addObject:imageView];
} 
else if (!imageView2.image) {
    imageView2.image = loadedImage;
    [self.array addObject:imageView2];
}

and so on.

sooper
  • 5,991
  • 6
  • 40
  • 65
  • Ah wow lol that makes sense. I haven't worked too much with `UIImageViews` and arrays, do you know how I should add them into my array in `viewDidLoad`? I was thinking it might look like this: `self.array = [[NSMutableArray arrayWithObjects: [UIImage imageNamed:@"Image1.png"], [UIImage imageNamed:@"Image2.png"], etc...` Would this be correct? – Henry F Apr 23 '12 at 17:56
  • Awesome thank you so much! So just to confirm, I would just need to add in `[self.array addObject:loadedImage];`. And, since I set the tags in IB, should the `UIImage's` be tagged automatically since they will jump into the already tagged `UIImageView`? Sorry for the novice questions again. – Henry F Apr 23 '12 at 18:00
  • My mistake, you want to add the `UIImageView` not the `UIImage`, check answer again. – sooper Apr 23 '12 at 18:05
  • Cool thank you so much, I just changed my array to an `NSMutableArray` and made those changes. So, I was wrong about the tags being automatically being placed lol so the app crashed. Now I'm just trying to figure out how to set the tags appropriately, I was thinking just under `[self.array addObject:loadedImage];` I should add in: `Tag *tagImages = [[Tag alloc] init]; for(NSInteger n = 0; n < [self.array count]; n++) { NSLog(@"%@", [self.array objectAtIndex:n]); }` Would this be correct? – Henry F Apr 23 '12 at 18:17
  • 1
    Check the answer again, I've changed it a bit, you're adding the `UIImageView` inside your `if` statements, not the `UIImage`. – sooper Apr 23 '12 at 18:18
  • Ah okay thanks, didn't see that earlier. I'm making those changes right now. So, should I still keep `[self.array addObject:loadedImage];`? And also, does my code for setting the tags in viewDidLoad look alright? Thanks again, this is incredibly helpful. – Henry F Apr 23 '12 at 18:21
  • You said that the tags are already set in Interface Builder, so there's no need (I'm not even sure `Tag` exists, is it your own class?). And no you don't need that line of code, you only want to add the `UIImageView`s. – sooper Apr 23 '12 at 18:23
  • Awesome this is working perfectly now. Thank you so much for all of your help and taking the time to explain everything. I couldn't of done it without your help, I really appreciate it! – Henry F Apr 23 '12 at 18:28