一、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];
}
网友评论