简体   繁体   English

从iPhone中的app目录中删除图像

[英]Delete image from app directory in iPhone

I want to delete an image from my iPhone app. 我想从我的iPhone应用程序中删除图像。 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. 最后两行显示了我的app目录中的内容,我想要删除的图像仍然存在。 What am I doing wrong? 我究竟做错了什么?

You are trying to delete a file in the Documents directory. 您正在尝试删除Documents目录中的文件。 You then read the contents of the bundle resources directory. 然后,您将阅读bundle资源目录的内容。 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. 如果您正在尝试删除Documents目录中的文件,那么您最后应该在NSLog()中放置该目录。 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: 你的代码看起来没问题,所以尝试在代码中添加一些'NSError'对象:

- (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. 在上面的代码中,我传递了一个NSError错误参数removeItemAtPath。 If the system can't delete the file, this method will return NO and fill the error object with the error raised. 如果系统无法删除该文件,则此方法将返回NO并填充error对象并引发错误。

Based on your comment I found out that you are trying to delete the default.png and replace it with another one. 根据您的评论,我发现您正在尝试删除default.png并将其替换为另一个。 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). image default.png是应用程序包的一部分,一旦创建和签名就无法修改(这是Apple的安全措施,因此应用程序在审核后无法更改)。 The only locations where you can create and delete files is inside the sandbox given to your application (the Documents folder). 您可以创建和删除文件的唯一位置是应用程序的沙箱(Documents文件夹)。

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

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