美文网首页iosGif动画
iOS GIF分解成uiimageView 播放GIF

iOS GIF分解成uiimageView 播放GIF

作者: 何大叔 | 来源:发表于2019-06-16 20:42 被阅读0次

一、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];

}

相关文章

网友评论

    本文标题:iOS GIF分解成uiimageView 播放GIF

    本文链接:https://www.haomeiwen.com/subject/mxqnfctx.html