将UIImage保存在Documents目录中

时间:2014-04-13 16:58:10

标签: ios uiimage

我知道这种方法可以将图像保存在文档目录中。

// Save image.
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];

但是想直接在文档目录中保存UIImage而不将其转换为NSData。有什么办法吗?

1 个答案:

答案 0 :(得分:1)

要将UIImage另存为图像格式的文件,请使用ImageIO framework

在此示例中,我将图像作为tiff保存到Application Support目录中(这是最快的,因为不涉及压缩):

CGImageSourceRef src = // ... whatever, depends on how you got the image originally
NSFileManager* fm = [NSFileManager new];
NSURL* suppurl = [fm URLForDirectory:NSApplicationSupportDirectory
                            inDomain:NSUserDomainMask
                   appropriateForURL:nil
                              create:YES error:nil];
NSURL* tiff = [suppurl URLByAppendingPathComponent:@"mytiff.tiff"];
CGImageDestinationRef dest =
    CGImageDestinationCreateWithURL((__bridge CFURLRef)tiff,
                                    (CFStringRef)@"public.tiff", 1, nil);
CGImageDestinationAddImageFromSource(dest, src, 0, nil);
bool ok = CGImageDestinationFinalize(dest);
// error-checking omitted
CFRelease(src); CFRelease(dest);