删除NSDocumentDirectory中的图像

时间:2012-06-18 01:58:00

标签: iphone ios xcode

我有一个自定义库,可以从相机胶卷中选取多个图像,我将图像保存在NSDocumentDirectory中,如下所示:

         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);

        }

我想要做的是我想在UIScrollView的缩略图中加载它,然后当点击特定图片时会弹出AlertView,会询问用户是否要删除它。我设法在缩略图视图中加载它(我使用HSImageSidebarView)并在用户点击图像时弹出AlertVIew弹出窗口。但是当我按下删除时,它会在视图中删除,但不会在NSDocumentDirectory中删除。

这是我删除图片的方式:

- (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");
}

1 个答案:

答案 0 :(得分:1)

在此行上,将%d替换为%lu

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