2017-04-18 101 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

相關問題