从iphone中的NSHomeDirectory()中删除图像

时间:2011-05-24 06:25:45

标签: iphone xcode

嗨,我正在保存这样的图片:

[UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
// Show the current contents of the documents folder
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);

- (NSString *) findUniqueSavePath
{   
    int i = 1;
    NSString *path;
    do 
    {
        // iterate until a name does not match an existing file
        path = [NSString stringWithFormat:@"%@/Documents/IMAGE-%d.PNG", NSHomeDirectory(), i++];
    } while ([[NSFileManager defaultManager] fileExistsAtPath:path]);

    [classObj updateimage];

    return path;
}

我想在文档中写入新图像时删除图像。

1 个答案:

答案 0 :(得分:2)

您可以使用此代码

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"image.png"];
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];