Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/r3econ/sprite-additions

Useful UIImage category for handling sprite sheets. This add-on smoothes the way of extracting images from a sprite sheet (texture atlas).
https://github.com/r3econ/sprite-additions

animation ios objective-c sprite spritesheet

Last synced: 10 days ago
JSON representation

Useful UIImage category for handling sprite sheets. This add-on smoothes the way of extracting images from a sprite sheet (texture atlas).

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/r3econ/UIImage-Sprite-Additions.svg?branch=master)](https://travis-ci.org/r3econ/UIImage-Sprite-Additions)
[![CodeFactor](https://www.codefactor.io/repository/github/r3econ/sprite-additions/badge)](https://www.codefactor.io/repository/github/r3econ/sprite-additions)
[![License](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://img.shields.io/badge/License-MIT-brightgreen.svg)
![Platform](https://img.shields.io/badge/platform-ios%20%7C%20tvos-lightgrey.svg)

UIImage Sprite Additions
=============

Useful `UIImage` category for handling sprite sheets. This add-on smoothes the way of extracting images from a sprite sheet (texture atlas).

This can be useful in `UIImageView` animations where animationImages array has to be filled with a set of images representing frames. `UIImage-Sprite` category makes this process trivial by introducing two methods:
```objective-c
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size;
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range spriteSize:(CGSize)size;
```
Quick start
=======

Just copy `UIImage+Sprite.h` and `UIImage+Sprite.m` to your project or use CocoaPods to install the pod.

Instructions for CocoaPods users:

1. Install [CocoaPods](http://cocoapods.org/) with `gem install cocoapods`.
2. Create a file in your XCode project called `Podfile` and add the following line:

pod 'UIImage+SpriteAdditions'

3. Run `pod install` in your xcode project directory. CocoaPods should download and
install the `UIImage+SpriteAdditions` category, and create a new Xcode workspace. Open up this workspace in Xcode.

How to
=======

There are two methods in `UIImage+SpriteAdditions` category. First one:
```objective-c
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size;
```
The method returns an array with `UIImage` objects. Original sprite sheet (image) is sliced into smaller chunks, each of the specified size.

Second method is very similar:
```objective-c
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range spriteSize:(CGSize)size;
```
This method does exactly the same thing as the latter. However, this time we can specify the range of the chunks we want to get.

Note that for performance reasons you should not fire these methods every time you want to get the array of animation frames. You should rather fire it once and store the output array somewhere. This is because the `CoreGraphics` image manipulation operations (especially on large images) are not so fast and your application may slow down.

I wrote this add-on to facilitate the setup of short animations using `UIImageView`. For more complex animations I recommend using `OpenGLES`.

Demo
=======

`SpriteAnimationDemo` project presents the usage of the `UIImage+Sprite` methods. The example shows how to create an animated `UIImageView`. This cool explosion sprite sheet which I included in the demo can be found [here](http://gushh.net/blog/free-game-sprites-explosion-3/). I added some numbers to this image to make testing and debugging easier.

![Imgur](https://i.imgur.com/lPNJnso.gif)

License
=======

This code is distributed under the terms and conditions of the MIT license.

Copyright (c) 2011 Rafal Sroka