YYImage
Image framework for iOS to display/encode/decode animated WebP, APNG, GIF, and more.
Top Related Projects
Asynchronous image downloader with cache support as a UIImageView category
A lightweight, pure-Swift library for downloading and caching images from the web.
AlamofireImage is an image component library for Alamofire
Promises for Swift & ObjC.
A delightful networking framework for iOS, macOS, watchOS, and tvOS.
Quick Overview
YYImage is a powerful image framework for iOS that supports animated WebP, APNG, and GIF formats. It provides high-performance image/GIF decoding, rendering, and encoding capabilities, making it an excellent choice for applications that require advanced image handling.
Pros
- Supports multiple animated image formats (WebP, APNG, GIF)
- High-performance decoding and rendering
- Provides both UIImage and CALayer extensions for easy integration
- Includes a custom image container format for better compression
Cons
- Limited to iOS platform
- May have a steeper learning curve compared to native iOS image handling
- Requires additional setup and integration steps
- Potential increase in app size due to additional libraries
Code Examples
- Loading and displaying an animated image:
YYImage *image = [YYImage imageNamed:@"animation.webp"];
YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
- Creating an animated image from data:
NSData *data = [NSData dataWithContentsOfFile:@"animation.apng"];
YYImage *image = [YYImage imageWithData:data];
- Encoding an image to WebP format:
UIImage *image = [UIImage imageNamed:@"example.png"];
NSData *webpData = [YYImageEncoder encodeImage:image type:YYImageTypeWebP quality:0.8];
- Using YYImage with UIImageView:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.yy_imageURL = [NSURL URLWithString:@"http://example.com/image.webp"];
[self.view addSubview:imageView];
Getting Started
- Add YYImage to your project using CocoaPods:
pod 'YYImage'
- Import YYImage in your source files:
#import <YYImage/YYImage.h>
- Use YYImage classes and methods in your code:
YYImage *image = [YYImage imageNamed:@"animation.gif"];
YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Competitor Comparisons
Asynchronous image downloader with cache support as a UIImageView category
Pros of SDWebImage
- More comprehensive image loading and caching solution
- Wider community support and active maintenance
- Extensive documentation and integration guides
Cons of SDWebImage
- Larger library size and potential overhead
- Steeper learning curve for basic implementations
Code Comparison
SDWebImage
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
YYImage
[imageView yy_setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
placeholder:[UIImage imageNamed:@"placeholder.png"]];
Key Differences
- SDWebImage offers more advanced features like progressive image loading and animated image support
- YYImage focuses on high-performance image rendering and decoding
- SDWebImage has a more extensive category system for UIImageView and other UI components
- YYImage provides better support for custom image formats and codecs
Use Cases
- SDWebImage: Ideal for projects requiring comprehensive image handling and caching
- YYImage: Better suited for applications with specific image format needs or performance-critical image rendering
Community and Support
SDWebImage has a larger user base and more frequent updates, while YYImage, though less actively maintained, still offers solid performance and specialized features.
A lightweight, pure-Swift library for downloading and caching images from the web.
Pros of Kingfisher
- More actively maintained with frequent updates and bug fixes
- Extensive documentation and community support
- Built-in support for image caching and memory management
Cons of Kingfisher
- Larger library size, which may impact app size
- Steeper learning curve for beginners due to more advanced features
Code Comparison
YYImage:
let imageView = YYAnimatedImageView()
imageView.yy_setImage(with: URL(string: "https://example.com/image.gif"))
Kingfisher:
let imageView = AnimatedImageView()
imageView.kf.setImage(with: URL(string: "https://example.com/image.gif"))
Key Differences
- YYImage focuses primarily on animated image handling, while Kingfisher offers a more comprehensive image loading and caching solution
- Kingfisher provides more advanced features like prefetching and image processing
- YYImage has a smaller footprint and may be more suitable for projects with simpler image loading requirements
Use Cases
- Choose YYImage for projects that primarily deal with animated images and require a lightweight solution
- Opt for Kingfisher when building apps that need robust image loading, caching, and processing capabilities
Both libraries offer efficient image handling for iOS applications, but Kingfisher provides a more feature-rich experience at the cost of increased complexity and library size.
AlamofireImage is an image component library for Alamofire
Pros of AlamofireImage
- Seamless integration with Alamofire networking library
- Built-in support for image caching and request prioritization
- Extensive image filtering and transformation capabilities
Cons of AlamofireImage
- Limited support for animated image formats (primarily focused on static images)
- Requires Alamofire as a dependency, which may not be ideal for all projects
- Less comprehensive support for various image formats compared to YYImage
Code Comparison
YYImage:
let image = YYImage(named: "animation.gif")
imageView.image = image
AlamofireImage:
let url = URL(string: "https://example.com/image.jpg")!
imageView.af.setImage(withURL: url)
Key Differences
- YYImage focuses on comprehensive image format support, including animated formats
- AlamofireImage is tailored for network image loading and processing
- YYImage provides more low-level control over image rendering and animation
- AlamofireImage offers better integration with network requests and caching
Both libraries have their strengths, with YYImage excelling in versatile image format support and AlamofireImage shining in network-based image loading and processing scenarios.
Promises for Swift & ObjC.
Pros of PromiseKit
- Simplifies asynchronous programming with a promise-based approach
- Extensive documentation and community support
- Supports multiple programming languages and platforms
Cons of PromiseKit
- Steeper learning curve for developers new to promise-based programming
- May introduce additional complexity for simple asynchronous tasks
- Potential performance overhead compared to traditional callback methods
Code Comparison
PromiseKit:
firstly {
fetchUser()
}.then { user in
fetchAvatar(user)
}.done { avatar in
self.imageView.image = avatar
}.catch { error in
print("Error: \(error)")
}
YYImage:
YYImage *image = [YYImage imageNamed:@"animation.gif"];
YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Summary
PromiseKit is a powerful library for managing asynchronous operations using promises, offering improved code readability and error handling. YYImage, on the other hand, focuses on image loading, caching, and animation, providing a simpler API for image-related tasks. While PromiseKit excels in complex asynchronous scenarios, YYImage offers more straightforward functionality for image manipulation and display.
A delightful networking framework for iOS, macOS, watchOS, and tvOS.
Pros of AFNetworking
- Comprehensive networking library for iOS and macOS
- Extensive documentation and community support
- Supports a wide range of networking tasks, including RESTful API interactions
Cons of AFNetworking
- Larger codebase and potentially higher memory footprint
- May be overkill for simple image loading tasks
- Less focused on image-specific optimizations
Code Comparison
YYImage (Image loading):
YYImage *image = [YYImage imageNamed:@"animation.gif"];
YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
AFNetworking (Image downloading):
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]];
[self.view addSubview:imageView];
Summary
YYImage is specifically designed for efficient image loading and animation, particularly for formats like GIF. It offers optimized performance for image-related tasks. AFNetworking, on the other hand, is a more comprehensive networking library that includes image downloading capabilities among its many features. While AFNetworking provides a broader range of networking functionalities, YYImage may be more suitable for projects primarily focused on image handling and animation.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
YYImage
Image framework for iOS to display/encode/decode animated WebP, APNG, GIF, and more.
(It's a component of YYKit)
Features
- Display/encode/decode animated image with these types:
WebP, APNG, GIF. - Display/encode/decode still image with these types:
WebP, PNG, GIF, JPEG, JP2, TIFF, BMP, ICO, ICNS. - Baseline/progressive/interlaced image decode with these types:
PNG, GIF, JPEG, BMP. - Display frame based image animation and sprite sheet animation.
- Dynamic memory buffer for lower memory usage.
- Fully compatible with UIImage and UIImageView class.
- Extendable protocol for custom image animation.
- Fully documented.
Usage
Display animated image
// File: ani@3x.gif
UIImage *image = [YYImage imageNamed:@"ani.gif"];
UIImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Display frame animation
// Files: frame1.png, frame2.png, frame3.png
NSArray *paths = @[@"/ani/frame1.png", @"/ani/frame2.png", @"/ani/frame3.png"];
NSArray *times = @[@0.1, @0.2, @0.1];
UIImage *image = [YYFrameImage alloc] initWithImagePaths:paths frameDurations:times repeats:YES];
UIImageView *imageView = [YYAnimatedImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Display sprite sheet animation
// 8 * 12 sprites in a single sheet image
UIImage *spriteSheet = [UIImage imageNamed:@"sprite-sheet"];
NSMutableArray *contentRects = [NSMutableArray new];
NSMutableArray *durations = [NSMutableArray new];
for (int j = 0; j < 12; j++) {
for (int i = 0; i < 8; i++) {
CGRect rect;
rect.size = CGSizeMake(img.size.width / 8, img.size.height / 12);
rect.origin.x = img.size.width / 8 * i;
rect.origin.y = img.size.height / 12 * j;
[contentRects addObject:[NSValue valueWithCGRect:rect]];
[durations addObject:@(1 / 60.0)];
}
}
YYSpriteSheetImage *sprite;
sprite = [[YYSpriteSheetImage alloc] initWithSpriteSheetImage:img
contentRects:contentRects
frameDurations:durations
loopCount:0];
YYAnimatedImageView *imageView = [YYAnimatedImageView new];
imageView.size = CGSizeMake(img.size.width / 8, img.size.height / 12);
imageView.image = sprite;
[self.view addSubview:imageView];
Animation control
YYAnimatedImageView *imageView = ...;
// pause:
[imageView stopAnimating];
// play:
[imageView startAnimating];
// set frame index:
imageView.currentAnimatedImageIndex = 12;
// get current status
image.currentIsPlayingAnimation;
Image decoder
// Decode single frame:
NSData *data = [NSData dataWithContentsOfFile:@"/tmp/image.webp"];
YYImageDecoder *decoder = [YYImageDecoder decoderWithData:data scale:2.0];
UIImage image = [decoder frameAtIndex:0 decodeForDisplay:YES].image;
// Progressive:
NSMutableData *data = [NSMutableData new];
YYImageDecoder *decoder = [[YYImageDecoder alloc] initWithScale:2.0];
while(newDataArrived) {
[data appendData:newData];
[decoder updateData:data final:NO];
if (decoder.frameCount > 0) {
UIImage image = [decoder frameAtIndex:0 decodeForDisplay:YES].image;
// progressive display...
}
}
[decoder updateData:data final:YES];
UIImage image = [decoder frameAtIndex:0 decodeForDisplay:YES].image;
// final display...
Image encoder
// Encode still image:
YYImageEncoder *jpegEncoder = [[YYImageEncoder alloc] initWithType:YYImageTypeJPEG];
jpegEncoder.quality = 0.9;
[jpegEncoder addImage:image duration:0];
NSData jpegData = [jpegEncoder encode];
// Encode animated image:
YYImageEncoder *webpEncoder = [[YYImageEncoder alloc] initWithType:YYImageTypeWebP];
webpEncoder.loopCount = 5;
[webpEncoder addImage:image0 duration:0.1];
[webpEncoder addImage:image1 duration:0.15];
[webpEncoder addImage:image2 duration:0.2];
NSData webpData = [webpEncoder encode];
Image type detection
// Get image type from image data
YYImageType type = YYImageDetectType(data);
if (type == YYImageTypePNG) ...
Installation
CocoaPods
- Update cocoapods to the latest version.
- Add
pod 'YYImage'
to your Podfile. - Run
pod install
orpod update
. - Import <YYImage/YYImage.h>.
- Notice: it doesn't include WebP subspec by default, if you want to support WebP format, you may add
pod 'YYImage/WebP'
to your Podfile.
Carthage
- Add
github "ibireme/YYImage"
to your Cartfile. - Run
carthage update --platform ios
and add the framework to your project. - Import <YYImage/YYImage.h>.
- Notice: carthage framework doesn't include WebP component, if you want to support WebP format, use CocoaPods or install manually.
Manually
- Download all the files in the YYImage subdirectory.
- Add the source files to your Xcode project.
- Link with required frameworks:
- UIKit
- CoreFoundation
- QuartzCore
- AssetsLibrary
- ImageIO
- Accelerate
- MobileCoreServices
- libz
- Import
YYImage.h
. - Notice: if you want to support WebP format, you may add
Vendor/WebP.framework
(static library) to your Xcode project.
FAQ
Q: Why I can't display WebP image?
A: Make sure you added the WebP.framework
in your project. You may call YYImageWebPAvailable()
to check whether the WebP subspec is installed correctly.
Q: Why I can't play APNG animation?
A: You should disable the Compress PNG Files
and Remove Text Metadata From PNG Files
in your project's build settings. Or you can rename your APNG file's extension name with apng
.
Documentation
Full API documentation is available on CocoaDocs.
You can also install documentation locally using appledoc.
Requirements
This library requires iOS 6.0+
and Xcode 8.0+
.
License
YYImage is provided under the MIT license. See LICENSE file for details.
ä¸æä»ç»
YYImage: åè½å¼ºå¤§ç iOS å¾åæ¡æ¶ã
(该项ç®æ¯ YYKit ç»ä»¶ä¹ä¸)
ç¹æ§
- æ¯æ以ä¸ç±»åå¨ç»å¾åçææ¾/ç¼ç /解ç :
WebP, APNG, GIFã - æ¯æ以ä¸ç±»åéæå¾åçæ¾ç¤º/ç¼ç /解ç :
WebP, PNG, GIF, JPEG, JP2, TIFF, BMP, ICO, ICNSã - æ¯æ以ä¸ç±»åå¾ççæ¸è¿å¼/éè¡æ«æ/éè¡æ«æ解ç :
PNG, GIF, JPEG, BMPã - æ¯æå¤å¼ å¾çææç帧å¨ç»ææ¾ï¼æ¯æåå¼ å¾çç sprite sheet å¨ç»ã
- é«æçå¨æå åç¼å管çï¼ä»¥ä¿è¯é«æ§è½ä½å åçå¨ç»ææ¾ã
- å®å ¨å ¼å®¹ UIImage å UIImageViewï¼ä½¿ç¨æ¹ä¾¿ã
- ä¿çå¯æ©å±çæ¥å£ï¼ä»¥æ¯æèªå®ä¹å¨ç»ã
- æ¯ä¸ªç±»åæ¹æ³é½æå®åçæ档注éã
ç¨æ³
æ¾ç¤ºå¨ç»ç±»åçå¾ç
// æ件: ani@3x.gif
UIImage *image = [YYImage imageNamed:@"ani.gif"];
UIImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
ææ¾å¸§å¨ç»
// æ件: frame1.png, frame2.png, frame3.png
NSArray *paths = @[@"/ani/frame1.png", @"/ani/frame2.png", @"/ani/frame3.png"];
NSArray *times = @[@0.1, @0.2, @0.1];
UIImage *image = [YYFrameImage alloc] initWithImagePaths:paths frameDurations:times repeats:YES];
UIImageView *imageView = [YYAnimatedImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
ææ¾ sprite sheet å¨ç»
// 8 * 12 sprites in a single sheet image
UIImage *spriteSheet = [UIImage imageNamed:@"sprite-sheet"];
NSMutableArray *contentRects = [NSMutableArray new];
NSMutableArray *durations = [NSMutableArray new];
for (int j = 0; j < 12; j++) {
for (int i = 0; i < 8; i++) {
CGRect rect;
rect.size = CGSizeMake(img.size.width / 8, img.size.height / 12);
rect.origin.x = img.size.width / 8 * i;
rect.origin.y = img.size.height / 12 * j;
[contentRects addObject:[NSValue valueWithCGRect:rect]];
[durations addObject:@(1 / 60.0)];
}
}
YYSpriteSheetImage *sprite;
sprite = [[YYSpriteSheetImage alloc] initWithSpriteSheetImage:img
contentRects:contentRects
frameDurations:durations
loopCount:0];
YYAnimatedImageView *imageView = [YYAnimatedImageView new];
imageView.size = CGSizeMake(img.size.width / 8, img.size.height / 12);
imageView.image = sprite;
[self.view addSubview:imageView];
å¨ç»ææ¾æ§å¶
YYAnimatedImageView *imageView = ...;
// æå:
[imageView stopAnimating];
// ææ¾:
[imageView startAnimating];
// 设置ææ¾è¿åº¦:
imageView.currentAnimatedImageIndex = 12;
// è·åææ¾ç¶æ:
image.currentIsPlayingAnimation;
//ä¸é¢ä¸¤ä¸ªå±æ§é½æ¯æ KVOã
å¾ç解ç
// 解ç å帧å¾ç:
NSData *data = [NSData dataWithContentsOfFile:@"/tmp/image.webp"];
YYImageDecoder *decoder = [YYImageDecoder decoderWithData:data scale:2.0];
UIImage image = [decoder frameAtIndex:0 decodeForDisplay:YES].image;
// æ¸è¿å¼å¾ç解ç (å¯ç¨äºå¾çä¸è½½æ¾ç¤º):
NSMutableData *data = [NSMutableData new];
YYImageDecoder *decoder = [[YYImageDecoder alloc] initWithScale:2.0];
while(newDataArrived) {
[data appendData:newData];
[decoder updateData:data final:NO];
if (decoder.frameCount > 0) {
UIImage image = [decoder frameAtIndex:0 decodeForDisplay:YES].image;
// progressive display...
}
}
[decoder updateData:data final:YES];
UIImage image = [decoder frameAtIndex:0 decodeForDisplay:YES].image;
// final display...
å¾çç¼ç
// ç¼ç éæå¾ (æ¯æåç§å¸¸è§å¾çæ ¼å¼):
YYImageEncoder *jpegEncoder = [[YYImageEncoder alloc] initWithType:YYImageTypeJPEG];
jpegEncoder.quality = 0.9;
[jpegEncoder addImage:image duration:0];
NSData jpegData = [jpegEncoder encode];
// ç¼ç å¨æå¾ (æ¯æ GIF/APNG/WebP):
YYImageEncoder *webpEncoder = [[YYImageEncoder alloc] initWithType:YYImageTypeWebP];
webpEncoder.loopCount = 5;
[webpEncoder addImage:image0 duration:0.1];
[webpEncoder addImage:image1 duration:0.15];
[webpEncoder addImage:image2 duration:0.2];
NSData webpData = [webpEncoder encode];
å¾çç±»åæ¢æµ
// è·åå¾çç±»å
YYImageType type = YYImageDetectType(data);
if (type == YYImageTypePNG) ...
å®è£
CocoaPods
- å° cocoapods æ´æ°è³ææ°çæ¬.
- å¨ Podfile ä¸æ·»å
pod 'YYImage'
ã - æ§è¡
pod install
æpod update
ã - å¯¼å ¥ <YYImage/YYImage.h>ã
- 注æï¼pod é
置并没æå
å« WebP ç»ä»¶, å¦æä½ éè¦æ¯æ WebPï¼å¯ä»¥å¨ Podfile ä¸æ·»å
pod 'YYImage/WebP'
ã
Carthage
- å¨ Cartfile ä¸æ·»å
github "ibireme/YYImage"
ã - æ§è¡
carthage update --platform ios
并å°çæç framework æ·»å å°ä½ çå·¥ç¨ã - å¯¼å ¥ <YYImage/YYImage.h>ã
- 注æï¼carthage framework 并没æå å« WebP ç»ä»¶ãå¦æä½ éè¦æ¯æ WebPï¼å¯ä»¥ç¨ CocoaPods å®è£ ï¼æè æå¨å®è£ ã
æå¨å®è£
- ä¸è½½ YYImage æ件夹å çææå 容ã
- å° YYImage å çæºæ件添å (ææ¾)å°ä½ çå·¥ç¨ã
- é¾æ¥ä»¥ä¸ frameworks:
- UIKit
- CoreFoundation
- QuartzCore
- AssetsLibrary
- ImageIO
- Accelerate
- MobileCoreServices
- libz
- 导å
¥
YYImage.h
ã - 注æï¼å¦æä½ éè¦æ¯æ WebPï¼å¯ä»¥å°
Vendor/WebP.framework
(éæåº) å å ¥ä½ çå·¥ç¨ã
常è§é®é¢
Q: 为ä»ä¹æä¸è½æ¾ç¤º WebP å¾çï¼
A: ç¡®ä¿ WebP.framework
å·²ç»è¢«æ·»å å°ä½ çå·¥ç¨å
äºãä½ å¯ä»¥è°ç¨ YYImageWebPAvailable()
æ¥æ£æ¥ä¸ä¸ WebP ç»ä»¶æ¯å¦è¢«æ£ç¡®å®è£
ã
Q: 为ä»ä¹æä¸è½ææ¾ APNG å¨ç»ï¼
A: ä½ åºè¯¥ç¦ç¨ Build Settings ä¸ç Compress PNG Files
å Remove Text Metadata From PNG Files
. æè
ä½ ä¹å¯ä»¥æ APNG æ件çæ©å±åæ¹ä¸ºapng
.
ææ¡£
ä½ å¯ä»¥å¨ CocoaDocs æ¥çå¨çº¿ API ææ¡£ï¼ä¹å¯ä»¥ç¨ appledoc æ¬å°çæææ¡£ã
ç³»ç»è¦æ±
该项ç®æä½æ¯æ iOS 6.0
å Xcode 8.0
ã
许å¯è¯
YYImage ä½¿ç¨ MIT 许å¯è¯ï¼è¯¦æ è§ LICENSE æ件ã
ç¸å ³é¾æ¥
Top Related Projects
Asynchronous image downloader with cache support as a UIImageView category
A lightweight, pure-Swift library for downloading and caching images from the web.
AlamofireImage is an image component library for Alamofire
Promises for Swift & ObjC.
A delightful networking framework for iOS, macOS, watchOS, and tvOS.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot