I want to create an animated Gif with true color from several PNG files represented as a base64 string. I found this article and did something similar. I have an array with dataUrls:
NSArray* imageDataUrls;
Here is what I did:
NSDictionary *fileProperties = @{ (__bridge id)kCGImagePropertyGIFDictionary: @{ (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever } }; NSDictionary *frameProperties = @{ (__bridge id)kCGImagePropertyGIFDictionary: @{ (__bridge id)kCGImagePropertyGIFDelayTime: @0.4f, // 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:@"animated.gif"]; CFMutableDataRef destinationData = CFDataCreateMutable(kCFAllocatorDefault, 0); CGImageDestinationRef destination = CGImageDestinationCreateWithData(destinationData, kUTTypeGIF, kFrameCount, NULL); CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties); NSData* myImageData; UIImage *myImage = [UIImage alloc]; for (NSUInteger i = 0; i < kFrameCount; i++) { @autoreleasepool { myImageData = [NSData dataFromBase64String:[imageDataUrls objectAtIndex:i]]; myImage = [myImage initWithData: myImageData]; CGImageDestinationAddImage(destination, myImage.CGImage, (__bridge CFDictionaryRef)frameProperties); } } myImageData = nil; myImage = nil; CFRelease(destination); NSData* data = nil; data = (__bridge NSData *)destinationData;
Finally, I send the gif image as base64EncodedString back to the phonegap container.
It works well, but the quality of the resulting gif is poor. This is due to the fact that it has only 256 colors.
Here is the original png image:

Here is a screenshot of the generated gif image:

How to get the same quality as imported, i.e. how to increase the quality level of the created gif? How can I generate true gifs on iOS?
ios objective-c iphone gif
confile Jul 02 '14 at 15:26 2014-07-02 15:26
source share