Displaying GIF Images on iOS Using ImageIO and FLAnimatedImage
This article explains how GIFs are structured, why iOS's default UIImage APIs cannot animate them, and provides a step‑by‑step guide using ImageIO to extract frames and timings, custom timer‑based playback, and the FLAnimatedImage library for efficient rendering and memory management.
GIF is a compressed image format that combines multiple static frames with timing information to create animation, and it is widely used across Android, iOS, and PC platforms.
Core principle : In iOS, loading a GIF with [UIImage imageNamed:name] or [UIImage imageWithData:data] returns only the first frame because the system UI components do not support animated GIFs directly.
Developers can use [imageView setAnimationImages:] together with [imageView setAnimationDuration:] to animate an array of images, but this approach cannot respect per‑frame delays and may consume excessive memory.
To obtain each frame and its display duration, the low‑level ImageIO framework is used. The following code demonstrates extracting an images array from GIF data:
// ImageIO example (pseudo‑code)
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)gifData, NULL);
size_t count = CGImageSourceGetCount(src);
NSMutableArray *frames = [NSMutableArray array];
for (size_t i = 0; i < count; i++) {
CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
NSDictionary *props = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(src, i, NULL));
NSNumber *delay = props[(NSString *)kCGImagePropertyGIFDelayTime];
// store img and delay
[frames addObject:@{@Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
