底层图像处理之GIF图片的合成与解析(三)(2)

本文详细介绍了如何在iOS开发中解析GIF图片,包括获取帧信息、延迟时间、循环次数,并演示了如何使用CGImageDestination创建和保存合成GIF。同时提及了作者对于Android开发者提升技能的资源分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

}
/* GIF图片解析 */
void getFrameInfo(NSString * string, NSMutableArray *frames, NSMutableArray *delayTimes, CGFloat *totalTime,CGFloat *gifWidth, CGFloat *gifHeight,NSUInteger loopCount)
{
NSData *data = [NSData dataWithContentsOfFile:string];
CGImageSourceRef gifSource = CGImageSourceCreateWithData((CFDataRef)data, nil);

//获取gif的帧数
size_t frameCount = CGImageSourceGetCount(gifSource);

//获取GfiImage的基本数据
NSDictionary *gifProperties = (__bridge NSDictionary ) CGImageSourceCopyProperties(gifSource, NULL);
//由GfiImage的基本数据获取gif数据
NSDictionary gifDictionary =[gifProperties objectForKey:(NSString)kCGImagePropertyGIFDictionary];
//获取gif的播放次数 0-无限播放
loopCount = [[gifDictionary objectForKey:(NSString
)kCGImagePropertyGIFLoopCount] integerValue];
CFRelease((__bridge CFTypeRef)(gifProperties));

for (size_t i = 0; i < frameCount; ++i) {
//得到每一帧的CGImage
CGImageRef frame = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
[frames addObject:[UIImage imageWithCGImage:frame]];
CGImageRelease(frame);

//获取每一帧的图片信息
NSDictionary frameDict = (__bridge NSDictionary)CGImageSourceCopyPropertiesAtIndex(gifSource, i, NULL);

//获取Gif图片尺寸
if (gifWidth != NULL && gifHeight != NULL) {
gifWidth = [[frameDict valueForKey:(NSString)kCGImagePropertyPixelWidth] floatValue];
gifHeight = [[frameDict valueForKey:(NSString)kCGImagePropertyPixelHeight] floatValue];
}
if (frameCount != 1) {

//由每一帧的图片信息获取gif信息
NSDictionary gifDict = [frameDict valueForKey:(NSString)kCGImagePropertyGIFDictionary];
//取出每一帧的delaytime
[delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]];

if (totalTime) {
*totalTime = totalTime + [[gifDict valueForKey:(NSString)kCGImagePropertyGIFDelayTime] floatValue];
}
}

CFRelease((__bridge CFTypeRef)(frameDict));
}
CFRelease(gifSource);
}

根据相关参数设置合成GIF注意也可以直接生成data而不是保存到沙盒

NSArray *delays = [gifDicInfo objectForKey:@“delays”];

//是否循环
NSUInteger loopCount = [[gifDicInfo objectForKey:@“loopCount”] integerValue];

//创建图片路径
NSString *cashPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@“animated11.gif”];

NSURL *url = [NSURL fileURLWithPath:cashPath];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)url, kUTTypeGIF, images.count, NULL);
// NSMutableData *data = [NSMutableData data];
// CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)data, kUTTypeGIF, images.count, NULL);
NSDictionary *gifDic = @{
(NSString *)kCGImagePropertyGIFLoopCount:[NSNumber numberWithInteger:loopCount]
};
NSDictionary *gifProperties = @{
(NSString *)kCGImagePropertyGIFDictionary:gifDic
};

for (int i = 0; i < images.count; i++) {

UIImage *image = [images objectAtIndex:i];
NSDictionary *gifDict = @{
(NSString *)kCGImagePropertyGIFDelayTime:[delays objectAtIndex:i]

};
NSDictionary *frameProperties = @{
(NSString *)kCGImagePropertyGIFDictionary:gifDict
};
CGImageDestinationAddImage(destination, image.CGImage, (CFDictionaryRef)frameProperties);
CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);

}
CGImageDestinationFinalize(destination);

CFRelease(destination);

NSLog(@“animated GIF file created at %@”, cashPath);
NSMutableArray *array = [NSMutableArray array];
NSData *data = [NSData dataWithContentsOfFile:cashPath];
[array addObject:data];
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@“group.adfasf”];
[shared setObject:[array copy] forKey:@“data123”];
[shared synchronize];

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

下面是辛苦给大家整理的学习路线,有需要的可以点击这里免费获取

下面是辛苦给大家整理的学习路线,有需要的可以点击这里免费获取
[外链图片转存中…(img-6hGukdCw-1710699289151)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值