[英]Save UIImage in Documents directory
我知道這種將圖像保存在文檔目錄中的方法。
// Save image.
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
但是要直接將UIImage
保存在Document目錄中而不將其轉換為NSData
。 有什么辦法嗎?
要將UIImage保存為圖像格式的文件,請使用ImageIO框架 。
在此示例中,我將圖像以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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.