简体   繁体   中英

Delete image from app directory in iPhone

I want to delete an image from my iPhone app. I use the method below, passing the name of the image as an argument.

The problem is that the image isn't deleted.

- (void)removeImage:(NSString*)fileName {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
                      [NSString stringWithFormat:@"%@.png", fileName]];

    [fileManager removeItemAtPath: fullPath error:NULL];
    NSLog(@"image removed: %@", fullPath);

    NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];    
    NSLog(@"Directory Contents:\n%@", [fileManager directoryContentsAtPath: appFolderPath]);
}

The last two lines show the content in my app directory and the image I want to delete is still there. What am I doing wrong?

You are trying to delete a file in the Documents directory. You then read the contents of the bundle resources directory. These are not the same directory.

If you're trying to delete a file in the Documents directory, the you should rad that directory in your NSLog() at the end. If you're trying to delete a file inside your bundle, this is impossible. App bundles are signed and cannot be modified.

your code looks ok, so try adding some 'NSError' object to you code:

- (void)removeImage:(NSString*)fileName {

   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,   YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
                      [NSString stringWithFormat:@"%@.png", fileName]];

   NSError *error = nil;
   if(![fileManager removeItemAtPath: fullPath error:&error]) {
      NSLog(@"Delete failed:%@", error);
   } else {
      NSLog(@"image removed: %@", fullPath);
   }

   NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];    
   NSLog(@"Directory Contents:\n%@", [fileManager directoryContentsAtPath: appFolderPath]);
}

In the code above I passed a NSError the error parameter of removeItemAtPath. If the system can't delete the file, this method will return NO and fill the error object with the error raised.

Based on your comment I found out that you are trying to delete the default.png and replace it with another one. Unfortunately, this is impossible. The image default.png is a part of your application bundle, which cannot be modified once it has been created and signed (this is a security measure from Apple, so applications cannot change after they have been reviewed). The only locations where you can create and delete files is inside the sandbox given to your application (the Documents folder).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM