在我的文档目录中,一个图像就是它们,名称为Image1。
我希望在文档目录文件夹中复制名称为Image2的相同图像。
请建议我怎么办?
答案 0 :(得分:3)
参考以下代码。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *imagePath1= [documentDirectory stringByAppendingPathComponent:@"image1.png"];
NSString *imagePath2= [documentDirectory stringByAppendingPathComponent:@"image2.png"];
[[NSFileManager defaultManager] copyItemAtPath:imagePath1 toPath:imagePath2 error:nil];
答案 1 :(得分:0)
从第一张图片中获取数据并使用其他名称保存相同的数据,如:
NSData *data = UIImageJPEGRepresentation(image1, 1.0);
NSString *str=@"image2.png"
NSString *fileName = [[stringPath stringByAppendingFormat:@"/"] stringByAppendingFormat:str];
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
[data writeToFile:fileName atomically:YES];
答案 2 :(得分:0)
我有两张Fetch图像并保存图像: 您获取保存在Document目录中的图像,然后传递它以使用不同的名称保存图像:
-(UIImage *)loadImage:(NSString *)name
{
NSString* documentsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
UIImage *res = [UIImage imageWithContentsOfFile:fullPath];
return res;
}
-(void)saveImage:(UIImage *)image withName:(NSString *)name
{
NSData *data = UIImageJPEGRepresentation(image, 1);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString* documentsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}