Reputation: 221
I am working on iOS - Cordova Cross platform .
I am getting image path from Javascript side using below code -
NSString *fileName =[command argumentAtIndex:0];
and i want to delete image from this image path Using Objective-c.
What should i do ? Please help
Thanks
Upvotes: 1
Views: 131
Reputation: 221
-(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);
}
}
Upvotes: 0
Reputation: 3023
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];
Upvotes: 1