如何使用Objective-C从IOS Cordova中的设备(本地数据库)中删除图像

时间:2016-03-04 06:35:24

标签: javascript ios objective-c cordova

我正在开发 iOS - Cordova 跨平台。

我正在使用以下代码从Javascript端获取图像路径 -

 NSString *fileName =[command argumentAtIndex:0];

我希望使用Objective-c从此图像路径中删除图像。

我该怎么办?请帮忙

由于

2 个答案:

答案 0 :(得分:1)

你应该能够这样做:

NSString *fileName = [command argumentAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", fileName]];
[fileManager removeItemAtPath:filePath error:NULL];

答案 1 :(得分:0)

 -(void)deleteImageFromDevice:(CDVInvokedUrlCommand*)command
 {
   @try
   {
       //File Name Getting from JS side
       NSString *fileName = [command argumentAtIndex:0];

      //Make a component Sepratation of File Name
      NSArray *items = [fileName componentsSeparatedByString:@"/"];

      //Get File(Image) At last index
      NSString* FileAtLastIndex =[items objectAtIndex:([items count]-1)];

      //creating file manager
      NSFileManager *fileManager = [NSFileManager defaultManager];

      //Create a Local Path
      NSString *documentPath =[NSSearchPathForDirectoriesInDomains
                              (NSDocumentDirectory, NSUserDomainMask, YES)
                              objectAtIndex:0];

     //Appending the IconImages Folder In Local Path
     NSString *filePath = [documentPath
                          stringByAppendingPathComponent:@"IconImages"];

      //Appending The File(Image), we get it from array Last index
      NSString *filePathAtLastIndex =
                [filePath stringByAppendingPathComponent:FileAtLastIndex];

      NSError *error=nil;

      //Checking that File is removed or Not.
      BOOL success =
            [fileManager removeItemAtPath:filePathAtLastIndex error:&error];

      //If file is Removed
        if (!success)
        {
          NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
        }
     }
  @catch(NSException *exception)
  {
      NSLog(@"DeleteImageFromDevice exception %@",exception);
  }
 }