简体   繁体   English

删除app文档目录中的图像

[英]Delete image in app document directory

I have 24 images in a "PhotoAlbum" folder in my app document directory. 我的app文档目录中的“PhotoAlbum”文件夹中有24个图像。 How to delete a selected image in the "PhotoAlbum"? 如何删除“PhotoAlbum”中的所选图像? How do I pass the object index? 如何传递对象索引?

I have something like this: 我有这样的事情:

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
    {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/PhotoAlbum"];

    NSString *fullPath = [dataPath stringByAppendingPathComponent:[self.photos objectAtIndex:index]];
    NSData *pngData = [NSData dataWithContentsOfFile:fullPath];
    UIImage *image = [UIImage imageWithData:pngData];

    MWPhoto *photo = [[MWPhoto alloc] initWithImage:image];
    return photo;
}

You can't delete it by specifying an index. 您无法通过指定索引来删除它。 You have to specify the image name you wanted to delete. 您必须指定要删除的图像名称。 It will be something like this 它会是这样的

NSString *filePath = [NSString stringWithFormat:@"%@/%@",documentPath,image.imageName];

    NSError *error = nil;
    if ([[NSFileManager defaultManager] fileExistsAtPath:documentPath]){
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}

This will delete your image and return deleted image. 这将删除您的图像并返回已删除的图像。

 - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
    {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/PhotoAlbum"];

    NSString *fullPath = [dataPath stringByAppendingPathComponent:[self.photos objectAtIndex:index]];
    NSData *pngData = [NSData dataWithContentsOfFile:fullPath];
    UIImage *image = [UIImage imageWithData:pngData];

    MWPhoto *photo = [[MWPhoto alloc] initWithImage:image];
    if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]){
            [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
    }

    return photo;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除应用程序不应删除文档目录 - Deleting App should not delete Document Directory 从iPhone中的app目录中删除图像 - Delete image from app directory in iPhone 如何从 Gallery App IOS 内的文档目录显示图像? - How to show Image from document directory inside Gallery App IOS? 如果我将使用Swift 3再次运行该应用程序,则该图像在文档目录中不存在 - Image is not existing in document directory if I will run the app again using Swift 3 从 Document 目录中的目录中删除文件? - Delete files from directory inside Document directory? 图像未从文档目录中检索 - the image is not retrieving from document directory 相机图像未保存在文档目录中 - Camera image not saving in document directory 将图像文件保存到Swift中的应用程序文档目录中。 即使部署在设备上,App文档目录的url也会更改吗? - Save image file to app documents directory in Swift. Does the url to App document directory change even when deployed on the device? 从文档目录中删除指定文件 - Delete specified file from document directory 当我在滚动UITableview期间显示文档目录中的图像时,内存不断增加然后应用程序崩溃 - Memory continuously increase then app crash when i display image from document directory during scrolling UITableview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM