Bazinga
Bazinga

Reputation: 2466

Delete an image in NSDocumentDirectory

I have a custom library that can pick mulitple images from the camera roll, I save my images in the NSDocumentDirectory like this:

         for (int i = 0; i < info.count; i++) {
            NSLog(@"%@", [info objectAtIndex:i]);
            NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,    NSUserDomainMask ,YES );
            NSString *documentsDir = [paths objectAtIndex:0];
            NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
            ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
            UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
            //----resize the images
            image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(256,256*image.size.height/image.size.width)];

            NSData *imageData = UIImagePNGRepresentation(image);
            [imageData writeToFile:savedImagePath atomically:YES];

            NSLog(@"saving at:%@",savedImagePath);

        }

What I wanted to do is I wanted to load it in a thumbnail with UIScrollView then when a specific picture is tapped a AlertView will popup,will ask the user if it wants to delete it. I manage to make it load in a thumbnail view (I use theHSImageSidebarView) and make the AlertVIew popups whenever a user taps in a image. But when I press delete it deletes in the view but not in the NSDocumentDirectory.

This is how I delete my image:

- (void)sidebar:(HSImageSidebarView *)sidebar didRemoveImageAtIndex:(NSUInteger)anIndex {
    NSLog(@"Image at index %d removed", anIndex);
    [images removeObjectAtIndex:anIndex];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", anIndex]];
    [fileManager removeItemAtPath: fullPath error:NULL];
    NSLog(@"image removed");
}

Upvotes: 0

Views: 492

Answers (1)

user523234
user523234

Reputation: 14834

On this line, replace %d with %lu

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", anIndex]];

Upvotes: 1

Related Questions