简体   繁体   中英

Save UIImage in Documents directory

I know this way to save image in document directory.

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

But want to save UIImage in Document directory directly without converting it to NSData . Is there any way?

To save a UIImage as a file in an image format, use the ImageIO framework .

In this example, I save an image into the Application Support directory as a tiff (this is fastest because no compression is involved):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM