您可以使用Image I/O框架(iOS SDK的一部分)创建GIF动画.您还需要包括MobileCoreServices
框架,它定义了GIF类型常量.您需要将这些框架添加到您的目标,并将它们的头导入要创建动画GIF的文件中,如下所示:
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
用例子来解释是最容易的.我会给你看我用来在我的iPhone5上制作这个GIF的代码:
首先,下面是一个辅助函数,它接受一个大小和一个Angular ,并在该Angular 返回红盘的UIImage
:
static UIImage *frameImage(CGSize size, CGFloat radians) {
UIGraphicsBeginImageContextWithOptions(size, YES, 1); {
[[UIColor whiteColor] setFill];
UIRectFill(CGRectInfinite);
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(gc, size.width / 2, size.height / 2);
CGContextRotateCTM(gc, radians);
CGContextTranslateCTM(gc, size.width / 4, 0);
[[UIColor redColor] setFill];
CGFloat w = size.width / 10;
CGContextFillEllipseInRect(gc, CGRectMake(-w / 2, -w / 2, w, w));
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
现在我们可以创建GIF了.首先,我们将为帧数定义一个常量,因为我们以后需要它两次:
static void makeAnimatedGif(void) {
static NSUInteger const kFrameCount = 16;
我们需要一个属性字典来指定动画应该重复的次数:
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
}
};
我们还需要另一个属性字典,我们将把它附加到每个框架,指定框架应该显示多长时间:
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
}
};
我们还将在文档目录中创建GIF的URL:
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];
现在,我们可以创建一个将GIF写入指定URL的CGImageDestination
:
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
我发现将fileProperties
作为CGImageDestinationCreateWithURL
的最后一个参数可以做到not.你得用CGImageDestinationSetProperties
.
现在我们可以创建和编写框架:
for (NSUInteger i = 0; i < kFrameCount; i++) {
@autoreleasepool {
UIImage *image = frameImage(CGSizeMake(300, 300), M_PI * 2 * i / kFrameCount);
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
}
}
请注意,我们将帧属性字典与每个帧图像一起传递.
在我们准确添加了指定数量的帧之后,我们最终确定了目标并将其释放:
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"failed to finalize image destination");
}
CFRelease(destination);
NSLog(@"url=%@", fileURL);
}
如果在模拟器上运行此命令,则可以从调试控制台复制URL并将其粘贴到浏览器中以查看图像.如果您在设备上运行它,您可以使用Xcode管理器窗口从设备下载应用程序沙箱并查看图像.或者你可以使用像iExplorer
这样的应用程序,让你直接浏览设备的文件系统.(这不需要越狱.)
我在运行iOS 6.1的iPhone 5上测试了这一点,但我相信代码应该可以追溯到iOS 4.0.
为了便于复制,我把所有代码都写进了this gist.