使用 UIImages 创建 gif

Tej*_*uri 4 objective-c gif cgimage ios

我指的是这篇文章。我正在尝试使用屏幕截图创建的图像制作一个 gif 文件。我正在使用计时器来创建屏幕快照,以便获得可用于创建 gif 所需的帧数。我每 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();

}
Run Code Online (Sandbox Code Playgroud)

我所指的帖子显示了创建 gif 的辅助函数。我不确定应该如何将图像传递给辅助函数。这是我尝试过的:

我尝试修改这部分:

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

这将创建包含 10 帧我的 gif 图像UIView

现在....

我想做的是:

UIView我正在用手指绘制一个简单的图画UIBeizerPath,并且在绘制的同时拍摄快照,这样我将拥有大约 50-100 个 PNG 文件。我试图将所有这些图像传递给 makeGifMethod。

工作流程:

  1. 启动应用程序。
  2. 点击启动计时器的按钮,每 0.1 秒拍照一次
  3. 用手指绘图(因此所有绘图将每 0.1 秒捕获一次)
  4. 在每个快照之后,我调用makeanimatedgif方法,以便将拍摄的快照添加到 gif 文件中的前一帧
  5. 停止一切

问题:

-情况1:

  1. 点击按钮(立即创建 gif)
  2. 开始画画
  3. 停止
  4. 检查 gif(一个 10 帧的 gif 是用白色背景创建的,因为当我点击按钮时我什么也没画)

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

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

-案例2:

  1. 点击按钮(启动计时器,拍摄快照,并行调用 makeGifMethod)
  2. 画一些东西
  3. 停止
  4. 检查 gif(创建的空 gif 没有任何帧,我相信每 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);
}
Run Code Online (Sandbox Code Playgroud)

有人可以建议我如何将捕获的图像传递给上述方法来制作 gif 吗?

Tej*_*uri 5

经过几次解决之后,我选择将所有捕获的图像存储到一个数组中,并使用该数组将图像传递给 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]; 

} 
Run Code Online (Sandbox Code Playgroud)

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

后来使用了相同的数组:

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

  }
Run Code Online (Sandbox Code Playgroud)