史上最简单的GIF制作(录屏)---一行代码搞定(OC)

之前无意间看到优酷视频有个GIF截取的功能,感觉相当炫酷,于是自己就想着有空搞一个,前一阵子抽空查资料发现OC原生的有相关的方法,就整理了下,然后又忘了分享出来,这不,又看到了,所以这次还是po出来,让大家看看,相互进步吧。

我叫杜甫,我很忙.png

先把调用的那行代码拿出来,让大家看的省心点(免得说楼主骗人,说好的一行代码,吧嗒吧嗒这么久?)

示例--已经是录好的gif

//   web就是要录制gif的view
    [screenShotToll RecordScreenInView:web During:5 gifPath:^(NSString *gifsPath) {
        
//        获取到gif保存的地址,可以显示可以储存
        NSLog(@"%@",gifsPath);
//        显示
        UIWebView *webs = [[UIWebView alloc] init];
        webs.frame = CGRectMake(0, sheight / 2,swidth, sheight / 2);
        [self.view addSubview:webs];
        NSData *gifData = [NSData dataWithContentsOfFile:gifsPath];
        [webs loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
//        储存
        NSData *data = [NSData dataWithContentsOfFile:gifsPath];
//         保存到本地相册  ALAssetsLibrary需要导入头文件
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
            NSLog(@"Success at %@", [assetURL path] );
        }] ;
    }];
首先,乍一听软件录屏(GIF制作)好像特别高大上,感觉无从下手,分析开来,gif都是由一帧一帧的图片组合而成,经过多张图片的处理最后形成新的文件格式.gif。那么单张图片怎么获取呢?答案很简单,原生代码撸几行就好了,如下:返回的就是屏幕截图或者可以是屏幕内任意view的截图
+ (UIImage *)shotInView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

接下来就要获取到录屏时间内的所有截图用于制作gif
- (void)writeImageToHD:(NSInteger)time inView:(UIView *)view writeOk:(void (^)(BOOL isOK))writeOK
{
//    创建gif图的文件保存目录
    __block NSString *imagePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    imagePath = [imagePath stringByAppendingPathComponent:@"gif"];
    [[screenShotToll shareInstance].manager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
    __block int i = 1;
//    为即将保存的图片设置图片名称
    __block NSString *imagePS = [imagePath stringByAppendingString:[NSString stringWithFormat:@"/image-%d.png",i]];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
//        将定时器时间内每隔.1(截图频率,自己可以控制)的屏幕截图储存起来,保存成功后进行下一张存放
        if ([UIImagePNGRepresentation([screenShotToll shotInView:view]) writeToFile:imagePS atomically:YES] && i  < (time / .1)) {
            i ++;
            imagePS = [imagePath stringByAppendingString:[NSString stringWithFormat:@"/image-%d.png",i]];
        }else{
//            屏幕截图完成后返回YES,图片数量 = time / 截图频率
            [timer invalidate];
            timer = nil;
            writeOK(YES);
        }
    }];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

最后就是重头戏,将上述截图完成的图片们组合成一个完整的GIF
+ (void)RecordScreenInView:(UIView *)view During:(NSInteger)time gifPath:(void (^)(NSString *gifsPath))gifsPath
{
    
    [[screenShotToll shareInstance] writeImageToHD:time inView:view writeOk:^(BOOL isOK) {
        if (isOK) {
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
            path = [path stringByAppendingPathComponent:@"/gif"];
//            创建数组保存所有图片
            NSMutableArray *images = [NSMutableArray array];
            for (int j = 1; j < time / .1; j ++) {
                UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/image-%d.png",path,j]];
                if (image) {
                    [images addObject:image];
                }
            }
            CGImageDestinationRef destination;
            NSString *doucument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//            设置gif图的储存地址
            NSString *gif = [doucument stringByAppendingPathComponent:@"gif"];
            [[screenShotToll shareInstance].manager createDirectoryAtPath:gif withIntermediateDirectories:YES attributes:nil error:nil];
//            获取gif的储存地址 --- 再往下的是我在网上扒了好久扒到的
            NSString *gifPath = [gif stringByAppendingPathComponent:@"image.gif"];
            CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)gifPath, kCFURLPOSIXPathStyle, false);
            destination = CGImageDestinationCreateWithURL(url,kUTTypeGIF, images.count, NULL);
//            接下来配置制作gif时的一些属性
            NSDictionary *framePro = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:.5],(NSString *)kCGImagePropertyGIFDelayTime, nil] forKey:(NSString *)kCGImagePropertyGIFDictionary];
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
            [dict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
            [dict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
            [dict setObject:(NSString *)kCGImagePropertyGIFImageColorMap forKey:(NSString *)kCGImagePropertyColorModel];
            [dict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
            
//            遍历截图数组,制作gif图
            NSDictionary *gifPro = [NSDictionary dictionaryWithObject:dict forKey:(NSString *)kCGImagePropertyGIFDictionary];
            for (UIImage *image in images) {
                CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)framePro);
            }
            CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifPro);
            CGImageDestinationFinalize(destination);
            CFRelease(destination);
            
            gifsPath(gifPath);
        }
    }];
}

好了留下demo,觉得好的可以给星啊!谢谢大家支持

你可能感兴趣的:(史上最简单的GIF制作(录屏)---一行代码搞定(OC))