首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在NSDocumentDirectory中删除图像

在NSDocumentDirectory中删除图像
EN

Stack Overflow用户
提问于 2012-06-18 09:58:00
回答 1查看 486关注 0票数 0

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

代码语言:javascript
复制
         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弹出窗口。但当我按delete时,它会在视图中删除,但不会在NSDocumentDirectory中删除。

这就是我如何删除我的镜像:

代码语言:javascript
复制
- (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");
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-18 10:58:35

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

代码语言:javascript
复制
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", anIndex]];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11076204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档