Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top