简体   繁体   English

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

[英]How to delete a image from Device (Local DB) in IOS Cordova using Objective-C

I am working on iOS - Cordova Cross platform . 我正在使用iOS-Cordova Cross平台。

I am getting image path from Javascript side using below code - 我正在使用以下代码从Javascript端获取图像路径-

 NSString *fileName =[command argumentAtIndex:0];

and i want to delete image from this image path Using Objective-c. 我想使用Objective-c从此图像路径中删除图像。

What should i do ? 我该怎么办 ? Please help 请帮忙

Thanks 谢谢

You should be able to do it like this: 您应该可以这样做:

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];
 -(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);
  }
 }

暂无
暂无

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

相关问题 在Cordova中从Objective-C调用JavaScript - Calling javascript from objective-c in cordova Cordova 4.4.0调用iOS Objective-C本机方法不起作用 - Cordova 4.4.0 call iOS Objective-C native method not working 如何将数据从 javascript 传递到 IOS Objective-c 中的 Z5A98E2840FD0141780D854E48C608D? - how to pass data from javascript to IOS Objective-c in webview? 如何使用evaluateJavaScript 将数据从WKWebview 发送到HTML 文件 | iOS | 目标-C - How to send data from WKWebview to HTML file using evaluateJavaScript | iOS | Objective-C 如何使用本机Objective-C方法从本地HTML5文件中的javascript函数调用本机Objective-C方法,反之亦然? - How to call a native Objective-C method from javascript function in a local HTML5 file and vice-versa using native Objective-C method? 从Objective-C在iOS Web应用程序中调用Javascript - Calling Javascript in an iOS web app from Objective-C iOS从Javascript调用Objective-C:是否忽略了某些调用? - iOS Calling Objective-C from Javascript: Some calls are ignored? 如何在phonegap ios版本中从JavaScript调用Objective-C方法 - How to call a objective-C method from javascript in phonegap ios version 如何从 Javascript 调用 Objective-C 方法并将数据发送回 iOS 中的 Javascript? - How to invoke Objective-C method from Javascript and send back data to Javascript in iOS? 如何从UIWebView传递数据到Objective-C IOS中的本机应用程序 - How to get data from UIWebView pass to Native app in Objective-c IOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM