我使用下面的方法生成gif。
-(void)makeAnimatedGif:(NSArray *)imgArray {
__block NSString *fileName = [NSString stringWithFormat:@"%ld",(long)[[NSDate date] timeIntervalSince1970]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Do background work
NSUInteger kFrameCount = imgArray.count;
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
}
};
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
}
};
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
for (NSUInteger i = 0; i < kFrameCount; i++) {
@autoreleasepool {
UIImage *image =[imgArray objectAtIndex:i]; //Here is the change
if (image.CGImage) {
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
}
}
}
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"failed to finalize image destination");
}
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI
CFRelease(destination);
[self manageGifWithUrl:fileURL];
});
});
}我的gif的问题是尺寸太大了。生成的gif大小范围为40MB - 90MB。我找不到任何解决方案。
我的imgArray包含180张图片。有什么办法解决这个问题吗?
发布于 2017-11-28 08:30:17
如果要创建视频,请对视频进行编码。动画GIF对于像动画这样的视频封装来说是很糟糕的。它们之所以变得如此普遍,是因为每个浏览器都可以呈现它们。除非你的目标是跨平台的最小公分母回放,否则不要使用gif动画。
40-80MB听起来并不奇怪。要减小大小,请降低输入图像的分辨率--通过简单地使其更小或通过减小调色板以使其压缩得更好。
https://stackoverflow.com/questions/47500506
复制相似问题