crg*_*rgt 73 core-graphics objective-c animated-gif ios swift
我在iOS应用程序中有一系列用户自定义图像,这些图像以简单的逐帧翻书风格进行动画制作.
我的问题是:有没有办法让用户将动画导出为GIF动画?理想情况下,我想让他们通过电子邮件,社交分享(T/FB)或(最糟糕的情况......)将动画gif保存到他们的文档文件夹中,以便通过iTunes进行检索.
我知道如何将.png保存到照片库,我找到了一种将动画录制为QT文件的方法(http://www.cimgf.com/2009/02/03/record-your-core-animation - 动画/),但我还没有找到一种方法来踢出一个普通的老动画GIF.我在Core Animation或其他地方遗漏了什么吗?是否有任何人可以推荐的方法,框架或资源?对不起,如果问题太笼统 - 努力找到一个起点.
rob*_*off 160
您可以使用Image I/O框架(iOS SDK的一部分)创建动画GIF.您还需要包含MobileCoreServices
框架,该框架定义GIF类型常量.您需要将这些框架添加到目标,并将其标题导入要创建动画GIF的文件中,如下所示:
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
Run Code Online (Sandbox Code Playgroud)
通过示例解释最容易.我将向您展示我在iPhone 5上制作此GIF时使用的代码:
首先,这是一个辅助函数,它采用一个大小和一个角度,并以该角度返回一个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;
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以创建GIF了.首先,我们将为帧数定义一个常量,因为我们需要它两次:
static void makeAnimatedGif(void) {
static NSUInteger const kFrameCount = 16;
Run Code Online (Sandbox Code Playgroud)
我们需要一个属性字典来指定动画应该重复的次数:
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
}
};
Run Code Online (Sandbox Code Playgroud)
我们需要另一个属性字典,我们将附加到每个框架,指定该框架应显示多长时间:
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
}
};
Run Code Online (Sandbox Code Playgroud)
我们还将在文档目录中为GIF创建一个URL:
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];
Run Code Online (Sandbox Code Playgroud)
现在我们可以创建一个CGImageDestination
将GIF写入指定的URL:
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
Run Code Online (Sandbox Code Playgroud)
我发现,通过fileProperties
作为最后一个参数CGImageDestinationCreateWithURL
也没有工作.你必须使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我们将帧属性字典与每个帧图像一起传递.
在我们准确添加了指定数量的帧之后,我们最终确定了目标并将其释放:
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"failed to finalize image destination");
}
CFRelease(destination);
NSLog(@"url=%@", fileURL);
}
Run Code Online (Sandbox Code Playgroud)
如果在模拟器上运行此操作,则可以从调试控制台复制URL并将其粘贴到浏览器中以查看图像.如果您在设备上运行它,则可以使用Xcode Organizer窗口从设备下载应用程序沙箱并查看图像.或者您可以使用这样的应用程序iExplorer
,让您直接浏览设备的文件系统.(这不需要越狱.)
我在运行iOS 6.1的iPhone 5上测试了这个,但我相信代码应该可以像iOS 4.0一样工作.
我已将所有代码放在这个要点中以便于复制.
归档时间: |
|
查看次数: |
36462 次 |
最近记录: |