gpt4 book ai didi

ios - 将 UIImage 保存在 Documents 目录中

转载 作者:行者123 更新时间:2023-11-29 03:03:26 26 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

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

关于ios - 将 UIImage 保存在 Documents 目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23045573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com