iOS GIF分解成uiimageView 播放GIF

一、GIF分解成uiimageView

import <ImageIO/ImageIO.h>

import <MobileCoreServices/MobileCoreServices.h>

  • (void)deCompsitionGif{
    //1.拿到GIF数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"78" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    CGImageSourceRef sourse = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    //2.将GIF分解一帧帧
    size_t count = CGImageSourceGetCount(sourse);
    //NSLog(@"count: %@d",count);

    NSMutableArray *imageSources = [NSMutableArray array];
    self.imageSources = imageSources;

    for (NSInteger index = 0; index < count; index++) {

      CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourse, index, NULL);
      
      UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
      
      [imageSources addObject:image];
      
      CGImageRelease(imageRef);
    

    }

    CFRelease(sourse);

    int i = 0;
    for (UIImage *image in imageSources) {

      NSData *data = UIImagePNGRepresentation(image);
      
      NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *gifPath = path[0];
      NSString *pathnum = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
      i++ ;
      [data writeToFile:pathnum atomically:NO];
    

    }
    }

二播放GIF

  • (void)showGifEvent{
NSMutableArray *imageTemp = [NSMutableArray array];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 120, 120)];
[self.view addSubview:imageView];

for (NSInteger index = 0; index < 2; index++) {
    UIImage *image = self.imageSources[index];
    [imageTemp addObject:image];
}

[imageView setAnimationImages:imageTemp];
[imageView setAnimationRepeatCount:MAXFLOAT];
[imageView setAnimationDuration:1];
[imageView startAnimating];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容