imageIO介绍
image解压缩
图片帧延迟设置
bridge
gif生成
//gifPath gif图保存路径 images生成gif图的图片帧数组 return gif图保存路径
+ (NSString *)crectGIFWithGIFPath:(NSString *)gifPath images:(NSArray *)images {
//设置gif保存地址
CFURLRef urlRef = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (__bridge CFStringRef)gifPath, kCFURLPOSIXPathStyle, false);
CGImageDestinationRef destinationRef = CGImageDestinationCreateWithURL(urlRef, kUTTypeGIF, images.count, NULL);
//添加图片
NSDictionary *imageDict = @{(__bridge NSString *)kCGImagePropertyGIFDelayTime : @.5};//延迟多久播放下一帧图片
[images enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGImageDestinationAddImage(destinationRef, obj.CGImage, (__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImagePropertyGIFDictionary:imageDict});
}];
//设置gif属性
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
// [properties setObject:[NSNumber numberWithBool:YES] forKey:(__bridge NSString *)kCGImagePropertyGIFHasGlobalColorMap];//是否有全局颜色图
// [properties setObject:(__bridge NSString *)kCGImagePropertyColorModelRGB forKey:(__bridge NSString *)kCGImagePropertyColorModel];//颜色类型
// [properties setObject:[NSNumber numberWithInt:8] forKey:(__bridge NSString*)kCGImagePropertyDepth];//颜色深度
[properties setObject:@0 forKey:(__bridge NSString *)kCGImagePropertyGIFLoopCount];//是否重复 0表示无限循环下去
CGImageDestinationSetProperties(destinationRef, (__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImagePropertyGIFDictionary:properties});
//完成设置
CGImageDestinationFinalize(destinationRef);
//release
CFRelease(urlRef);
return gifPath;
}
gif解析
@implementation UIImageView (pyh_animation)
- (void)pyh_animationWithImageData:(NSData *)data {
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
size_t count = CGImageSourceGetCount(sourceRef);
//图片数组
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < count; i ++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourceRef, i, NULL);
[images addObject:[UIImage imageWithCGImage:imageRef]];
}
//属性
// NSDictionary *propertys = (__bridge_transfer NSDictionary *)(CGImageSourceCopyProperties(sourceRef, NULL));
// NSDictionary *imagePropertys = (__bridge_transfer NSDictionary *)(CGImageSourceCopyPropertiesAtIndex(sourceRef, 1, NULL));
CFDictionaryRef imagePropertys = CGImageSourceCopyPropertiesAtIndex(sourceRef, 1, NULL);
CFDictionaryRef gif = CFDictionaryGetValue(imagePropertys, kCGImagePropertyGIFDictionary);
NSNumber *delay = CFDictionaryGetValue(gif, kCGImagePropertyGIFDelayTime);
//动画执行
self.animationImages = images.copy;
self.animationRepeatCount = 0;
self.animationDuration = delay.floatValue * count;
CFRelease(sourceRef);
CFRelease(imagePropertys);
CFRelease(gif);
}
- (void)pyh_animationWithPath:(NSString *)path {
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (__bridge CFStringRef)path, kCFURLPOSIXPathStyle, false);
CGImageSourceRef sourceRef = CGImageSourceCreateWithURL(url, NULL);
size_t count = CGImageSourceGetCount(sourceRef);
//图片数组
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < count; i ++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourceRef, i, NULL);
[images addObject:[UIImage imageWithCGImage:imageRef]];
}
//属性
// NSDictionary *propertys = (__bridge_transfer NSDictionary *)(CGImageSourceCopyProperties(sourceRef, NULL));
// NSDictionary *imagePropertys = (__bridge_transfer NSDictionary *)(CGImageSourceCopyPropertiesAtIndex(sourceRef, 1, NULL));
CFDictionaryRef imagePropertys = CGImageSourceCopyPropertiesAtIndex(sourceRef, 1, NULL);
CFDictionaryRef gif = CFDictionaryGetValue(imagePropertys, kCGImagePropertyGIFDictionary);
NSNumber *delay = CFDictionaryGetValue(gif, kCGImagePropertyGIFDelayTime);
//动画执行
self.animationImages = images.copy;
self.animationDuration = delay.floatValue * count;
self.animationRepeatCount = 0;
[self startAnimating];
CFRelease(sourceRef);
CFRelease(imagePropertys);
CFRelease(gif);
}
@end
网友评论