gpt4 book ai didi

objective-c - 在 iOS10 上生成的 GIF 图像不再在浏览器上永远循环

转载 作者:太空狗 更新时间:2023-10-30 03:53:06 25 4
gpt4 key购买 nike

这是一个生成GIF图片的ObjectiveC方法:

@implementation NSArray (GIFImage)

- (NSString*)GIFImageWithImageDuration:(CGFloat)GIFImageDuration
{
NSArray *images = self;

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"animated.gif"];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
kUTTypeGIF,
images.count,
NULL);

NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:GIFImageDuration]
forKey:(NSString *)kCGImagePropertyGIFDelayTime]
forKey:(NSString *)kCGImagePropertyGIFDictionary];


NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
forKey:(NSString *)kCGImagePropertyGIFDictionary];

for ( NSInteger index = 0; index < images.count; index++ )
{
UIImage *image = (UIImage *)[images objectAtIndex:index];
CGImageDestinationAddImage(destination, image.CGImage, (CFDictionaryRef)frameProperties);
}

CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);
CGImageDestinationFinalize(destination);
CFRelease(destination);
NSLog(@"animated GIF file created at %@", path);
return path;
}

@end

在 iOS9 上运行此方法工作正常,GIF 在浏览器上永远生成循环:

enter image description here

在iOS10上运行同样的方法失败,生成的GIF不再在浏览器上永远循环,它只播放一次:

enter image description here

此外,受到这个答案的启发:https://stackoverflow.com/a/38082881/448662 ,我对iOS9和iOS10生成的两个GIF进行了十六进制比较,似乎iOS10生成的GIF中缺少一段(Netscape应用程序扩展),这可能是它不循环的原因,但我可以不确定。

简而言之,我们如何生成在浏览器上永远循环播放的 GIF 图像(使用 ObjectiveC)?

enter image description here

最佳答案

我在这个 GitHub 存储库的最近一次提交中找到了解决方案: https://github.com/NSRare/NSGIF/pull/23/files?diff=split

基本上,在 CGImageDestinationAddImage 之前调用 CGImageDestinationSetProperties 可以解决 iOS10 上的问题。

这是工作代码:

@implementation NSArray (GIFImage)
- (NSString*)GIFImageWithImageDuration:(CGFloat)GIFImageDuration
{
NSArray *images = self;

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"animated.gif"];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
kUTTypeGIF,
images.count,
NULL);

NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:GIFImageDuration]
forKey:(NSString *)kCGImagePropertyGIFDelayTime]
forKey:(NSString *)kCGImagePropertyGIFDictionary];


NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
forKey:(NSString *)kCGImagePropertyGIFDictionary];

CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);

for ( NSInteger index = 0; index < images.count; index++ )
{
UIImage *image = (UIImage *)[images objectAtIndex:index];
CGImageDestinationAddImage(destination, image.CGImage, (CFDictionaryRef)frameProperties);
}

CGImageDestinationFinalize(destination);
CFRelease(destination);
NSLog(@"animated GIF file created at %@", path);
return path;
}

关于objective-c - 在 iOS10 上生成的 GIF 图像不再在浏览器上永远循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40310243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com