简体   繁体   English

文档目录中的Imagepath替换

[英]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 . 使用NSFileManager ,如果存在图像路径或文件夹,如何替换文档目录中的新图像路径(或)文件夹。

Is there the replace feature in NSFileManager ? NSFileManager有替换功能吗? (how to replace image path position with new image path in document directory) (如何用文档目录中的新图像路径替换图像路径位置)

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];

You can not replace a path using NSFileManager because, you've already saved ImagePath. 您无法使用NSFileManager替换路径,因为您已经保存了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
    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];
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM