【问题标题】:How to delete a image from Device (Local DB) in IOS Cordova using Objective-C如何使用 Objective-C 从 IOS Cordova 中的设备(本地数据库)中删除图像
【发布时间】:2016-03-04 06:35:23
【问题描述】:

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

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

 NSString *fileName =[command argumentAtIndex:0];

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

我该怎么办?请帮忙

谢谢

【问题讨论】:

    标签: javascript ios objective-c cordova


    【解决方案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];
    

    【讨论】:

    • 谢谢,如果我想将 Base64 转换为 Path,我应该怎么做?请帮助@gvuksic
    • 我从 [command argumentAtIndex:0] 获取 Base64;那么如何将其转换为路径?我试过但它不起作用。请帮忙
    • 您正在获取 base64 数据? NSString *fileName = [[NSString alloc] initWithData:[command argumentAtIndex:0] encoding:NSUTF8StringEncoding];
    • @kishor NSString *fileName = [[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedString:[command argumentAtIndex:0] options:0] encoding:NSUTF8StringEncoding];
    【解决方案2】:
     -(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);
      }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 2012-07-21
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多