0

在我的文档目录中,有一个图像是他们的,名称为 Image1。

我想在文档目录文件夹中复制名称为 Image2 的相同图像。

请建议我该怎么做?

4

3 回答 3

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];
于 2012-08-01T07:37:14.063 回答
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];
于 2012-08-01T07:33:53.097 回答
0

我有两个 Fatch 图像和保存图像的数学运算:您将图像保存在 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];

}

于 2012-08-01T07:38:31.177 回答