Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

YYImage

Image framework for iOS to display/encode/decode animated WebP, APNG, GIF, and more.

Subscribe to updates I use YYImage


Statistics on YYImage

Number of watchers on Github 1149
Number of open issues 61
Average time to close an issue 17 days
Main language Objective-C
Average time to merge a PR 7 days
Open pull requests 9+
Closed pull requests 1+
Last commit about 2 years ago
Repo Created almost 4 years ago
Repo Last Updated over 1 year ago
Size 29.8 MB
Organization / Authoribireme
Latest Release1.0.3
Contributors2
Page Updated
Do you use YYImage? Leave a review!
View open issues (61)
View YYImage activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating YYImage for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

YYImage

License MIT  Carthage compatible  CocoaPods  CocoaPods  Support  Build Status

Image framework for iOS to display/encode/decode animated WebP, APNG, GIF, and more.
(It's a component of YYKit)

niconiconi~

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

  1. Update cocoapods to the latest version.
  2. Add pod 'YYImage' to your Podfile.
  3. Run pod install or pod update.
  4. Import <YYImage/YYImage.h>.
  5. 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

  1. Add github "ibireme/YYImage" to your Cartfile.
  2. Run carthage update --platform ios and add the framework to your project.
  3. Import <YYImage/YYImage.h>.
  4. Notice: carthage framework doesn't include WebP component, if you want to support WebP format, use CocoaPods or install manually.

Manually

  1. Download all the files in the YYImage subdirectory.
  2. Add the source files to your Xcode project.
  3. Link with required frameworks:
    • UIKit
    • CoreFoundation
    • QuartzCore
    • AssetsLibrary
    • ImageIO
    • Accelerate
    • MobileCoreServices
    • libz
  4. Import YYImage.h.
  5. 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 )

niconiconi~

==============

  • //:
        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

  1. cocoapods .
  2. Podfile pod 'YYImage'
  3. pod install pod update
  4. <YYImage/YYImage.h>
  5. pod WebP , WebP Podfile pod 'YYImage/WebP'

Carthage

  1. Cartfile github "ibireme/YYImage"
  2. carthage update --platform ios framework
  3. <YYImage/YYImage.h>
  4. carthage framework WebP WebP CocoaPods
  1. YYImage
  2. YYImage ()
  3. frameworks:
    • UIKit
    • CoreFoundation
    • QuartzCore
    • AssetsLibrary
    • ImageIO
    • Accelerate
    • MobileCoreServices
    • libz
  4. YYImage.h
  5. 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

==============

iOS Tip

YYImage open issues Ask a question     (View All Issues)
  • almost 3 years Use NSKeyedArchiver archiver YYAnimatedImageView have bug
  • almost 3 years AssetsLibrary和PhotoLibrary
  • almost 3 years YYCGImageCreateWithWebPData decode animated webp
  • almost 3 years 关于从制定bundle加载图片
  • almost 3 years How we can decode progressive jpeg image from internet?
  • almost 3 years 图片修复方向bug
  • about 3 years Why not cache images in YYFrameImage?
  • about 3 years 如何检测到动画结束
  • about 3 years 希望yy_saveToAlbumWithCompletionBlock 加个metadata参数
  • about 3 years YYImageCoder crash in iOS 10 beta3
  • about 3 years There is a way to stop webp animation?
  • about 3 years YYAnimatedImageView play animation from end to start?
  • about 3 years Frame duration issue on APNG?
  • about 3 years Crash app
  • about 3 years 关于webp的转码问题
  • about 3 years README typo
  • about 3 years Wrong code in YYImageCoder.m
  • about 3 years Support for `imageNamed`
  • about 3 years http://img1.imgtn.bdimg.com/it/u=1424299871,2560939022&fm=23.jpg
  • about 3 years YYAnimatedImageView 关于 loopcount
  • about 3 years Could not build Objective-C module and YYImage.h not found!
  • over 3 years some time times crashed when decode image.
YYImage open pull requests (View All Pulls)
  • Adds a callback at the end of a loop
  • Update YYImageCoder.h
  • * fixes #78 WebP encoding problem of images with certain dimensions?
  • Update README.md
  • Fixes a documentation issue
  • bug fix #81
  • Fix image Orientation
  • fix memory leak bug when _incrBufferCount < 0
  • Change gif loop behaviour to works like most of the modern browsers
YYImage list of languages used
YYImage latest release notes
1.0.3 Bug Fixes
  • Fix memory issue when play YYSpriteSheetImage with unreleased CG raster data.
  • Fix empty return value from YYCGImageCreateWithWebPData when decode webp frame with nonzero canvas offset. #41
  • Fix nil return value when encode animated GIF from data source. #44
1.0.2 Debug log and bugfix

Add debug log for WebP decoder when decode WebP without WebP subspec. Fix incorrectly GIF loop count value.

1.0.1 Bug fixes
  • Destroy pthread_mutex on dealloc
  • Avoid duplicate symbol error when use -all_load link flag #24
Other projects in Objective-C