2017-04-18 74 views
1

我在应用程序文档目录的“PhotoAlbum”文件夹中有24个图像。如何删除“PhotoAlbum”中的选定图像?我如何传递对象索引?在应用程序文档目录中删除图像

我有这样的事情:

- (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; 
} 
+0

ohk..create你的形象与独特的名字说myImage1,myImage2,myImage3,......等,并将它们保存在您的文件夹。这里1,2,3,...都是唯一的索引。现在只需激发选定索引图像的删除选项。希望你得到我 –

+0

看到这个快速教程:https://iosdevcenters.blogspot.com/2016/04/save-and-get-image-from-document.html –

回答

0

您无法通过指定索引中删除。您必须指定要删除的图像名称。它会是这样的

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

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

这将删除您的图像并返回已删除的图像。

- (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; 
} 
+0

嗨,如果我想把我的删除图像代码这个功能? - (void)photoBrowser :(MWPhotoBrowser *)photoBrowser deletebuttonPressedforPhotoAtIndex:(NSUInteger)index – Winter

相关问题