Rob 指出了在处理图像时需要考虑的许多非常好的事情(+1),但是这里有一个示例,说明如何按照您的要求创建 tiff 和 gif:
首先,您需要链接到ImageIO
库(在应用程序的构建阶段下)。
接下来,您需要#import <ImageIO/ImageIO.h>
在文件的顶部。
然后,以下代码将为您转换图像:
// Get a reference to the image that you already have stored on disk somehow.
// If it isn't stored on disk, then you can use CGImageSourceCreateWithData() to create it from an NSData representation of your image.
NSURL *url = [[NSBundle mainBundle] URLForResource:@"01" withExtension:@"jpg"];
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)(url), NULL);
// Create a URL referencing the Application Support Directory. We will save the new image there.
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *suppurl = [fm URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:NULL];
// Append the name of the output file to the app support directory
// For tiff change the extension in the next line to .tiff
NSURL *gifURL = [suppurl URLByAppendingPathComponent:@"mytiff.gif"];
// Create the destination for the new image
// For tiff, use @"public.tiff" for the second argument of the next line (instead of @com.compuserve.gif".
CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)gifURL,
(CFStringRef)@"com.compuserve.gif",
1,
NULL);
CGImageDestinationAddImageFromSource(dest, src, 0, NULL);
// Write the image data to the URL.
bool ok = CGImageDestinationFinalize(dest);
if (!ok)
NSLog(@"Unable to create gif file.");
// Cleanup
CFRelease(src);
CFRelease(dest);
这是改编自本书中的代码。