用UIImage创build一个gif

我指的是这个职位 。 我正在尝试使用截图创build的图像制作一个gif文件。 我正在使用计时器来创build屏幕的快照,以便获取可用于创buildgif的所需数量的帧。 我每隔0.1秒采取一次(我将在3秒后结束这个计时器)。

这里是我的代码来拍摄我的UIView的快照:

-(void)recordScreen{ self.timer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(takeSnapShot) userInfo:nil repeats:YES]; } -(void)takeSnapShot{ //capture the screenshot of the uiimageview and save it in camera roll UIGraphicsBeginImageContext(self.drawView.frame.size); [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } 

和我指的post,显示了一个帮助函数来创buildgif。 我不知道如何将我的图像传递给帮助函数。 这是我试过的:

我试图修改这部分:

  static NSUInteger kFrameCount = 10; for (NSUInteger i = 0; i < kFrameCount; i++) { @autoreleasepool { UIImage *image = [self takeSnaphot]; CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); } } 

这创造了我的UIView 10帧的gif。

现在….

我想要做的是:

我使用UIBeizerPath在我的UIView使用手指绘制了一个简单的绘图,并且我正在将快照与graphics并行处理,这样我就UIBeizerPath大约50-100个PNG文件。 我正在尝试将所有这些图像传递给makeGifMethod。

工作stream程:

  1. 启动应用程序。
  2. 点击启动定时器的button,每0.1秒拍摄一张照片
  3. 用手指画(因此所有的绘图都会被俘虏0.1秒)
  4. 在每个快照之后,我调用makeanimatedgif方法,以便拍摄的快照将被添加到gif文件中的前一帧
  5. 停止一切

问题:

案例1:

  1. 点击button(这确实创buildgif immediatley)
  2. 开始绘画
  3. 停止
  4. 检查GIF(与10帧的GIF创build与白色背景,因为我什么时候我按下button)

如果我在循环中调用snapShot方法,我会得到我绘图的最后10帧,但不是全部。

 for (NSUInteger i = 0; i < 10; i++) { @autoreleasepool { UIImage *image = [self takeSnapShot]; CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); } } 

案例2:

  1. 点击button(启动定时器,快照,并行调用makeGifMethod)
  2. 画一些东西
  3. 停止
  4. 检查GIF(空gif创build没有任何框架,我beleieve每0.1秒调用makeGifMethod没有按预期工作)

情况2的代码如下:

  -(void)recordScreen{ self.timer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(takeSnapShot) userInfo:nil repeats:YES]; } -(void)takeSnapShot{ //capture the screenshot of the uiimageview and save it in camera roll UIGraphicsBeginImageContext(self.drawView.frame.size); [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self makeAnimatedGif:(UIImage *)viewImage]; }); } -(void) makeAnimatedGif:(UIImage *)image{ 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 } }; documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]; fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"]; CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, 10, NULL); CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties); CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); if (!CGImageDestinationFinalize(destination)) { NSLog(@"failed to finalize image destination"); } CFRelease(destination); NSLog(@"url=%@", fileURL); } 

有人可以请build议我如何将捕获的图像传递到上面的方法来做一个GIF?

经过一些工作,我select将所有捕获的图像存储到一个数组中,并使用该数组将图像传递给gifMethod。 它工作真酷!

我已经将所有的图像存储到数组中:

 -(void)takeSnapShot{ //capture the screenshot of the uiimageview and save it in camera roll UIGraphicsBeginImageContext(self.drawView.frame.size); [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //store all the images into array [imgArray adDObject:viewImage]; } 

注意:确保在将图像存储到数组之前调整图像的大小,否则,如果长时间使用该图像,最终可能会出现内存警告,然后是应用程序崩溃。

后来使用了相同的数组:

 -(void)makeAnimatedGif { NSUInteger kFrameCount = imgArray.count; NSDictionary *fileProperties = @{ (__bridge id)kCGImagePropertyGIFDictionary: @{ (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever } }; NSDictionary *frameProperties = @{ (__bridge id)kCGImagePropertyGIFDictionary: @{ (__bridge id)kCGImagePropertyGIFDelayTime: @0.08f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data } }; NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]; NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"]; CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL); CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties); for (NSUInteger i = 0; i < kFrameCount; i++) { @autoreleasepool { UIImage *image =[imgArray objectAtIndex:i]; //Here is the change CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); } } if (!CGImageDestinationFinalize(destination)) { NSLog(@"failed to finalize image destination"); } CFRelease(destination); NSLog(@"url=%@", fileURL); }