[英]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.