Ravinder Reddy
Ravinder Reddy

Reputation: 15

Imagepath Replace in Document Directory

For using NSFileManager, if exists the Image path or folder,how to replace new image path (or) Folder in document directory .

Is there the replace feature in NSFileManager? (how to replace image path position with new image path in document directory)

Upvotes: 0

Views: 809

Answers (3)

rushisangani
rushisangani

Reputation: 3385

You can not replace a path using NSFileManager because, you've already saved ImagePath. Now you can check if that path is exists then you can remove that filePath using this:

if ([fileManager fileExistsAtPath:Path] == YES) {
  [fileManager removeItemAtPath:Path error:&error];
}

Now save your new path.

If you want to replace content then simple remove first then write again using

writeToFile:filePath

Upvotes: 1

metronic
metronic

Reputation: 455

You want to owerwrite an image, just write it to file. If an image with the same name exists at the same path, it will be overwritten:

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

UIImage * imageToSave = [UIImage imageNamed:@"Icon.png"];
NSData * binaryImageData = UIImagePNGRepresentation(imageToSave);

[binaryImageData writeToFile:[basePath  stringByAppendingPathComponent:@"myfile.png"] atomically:YES];

Upvotes: 1

kalpesh
kalpesh

Reputation: 1287

    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* foofile = [documentsPath stringByAppendingPathComponent:filename];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
    NSLog(fileExists ? @"Yes" : @"No");
    if (fileExists == YES)
    {
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", aDocumentsDirectory,filename];
        NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
        [data1 writeToFile:filePath atomically:YES];
    }

Upvotes: 0

Related Questions