Ron Yeh
Apply a simple rectangular mask for any SPImage.
image, crop, mask
BSD License


Use it like you would an SPImage, except you may specify a cropRect, which will allow you to crop/show a certain area of the image:

SPTexture *texture = [SPTexture textureWithContentsOfFile:@"Image.png"];
SXCroppedImage *img = [[[SXCroppedImage alloc] initWithTexture:texture] autorelease];
img.cropRect = CGRectMake(230, 240, 90, 48);
[self addChild:img];

The coordinates are relative to the top-left physical corner of your iPhone/iPad.

Source Code

SXCroppedImage subclasses SPImage. You can take a similar approach if you want to mask other SPDisplayObjects, such as SPQuad. You might need to add more imports, depending on how you set up your project and how you reference the Sparrow framework.

Header (SXCroppedImage.h)

#import <Foundation/Foundation.h>
#import "SPImage.h"
@class SPTexture;
@interface SXCroppedImage : SPImage {
    int mCropX; // these coordinates are in points (320 x 480 for iPhone)
    int mCropY;
    int mCropW;
    int mCropH;
    int mCropXPixels;        // However, GL_SCISSOR_TEST needs absolute pixels
    int mCropYPixelsFlipped; // We also flip the y axis so that our offsets are from the top-left corner of the iPhone
    int mCropWPixels;
    int mCropHPixels;
    float mScreenHeightPixels;
@property (nonatomic, assign) CGRect cropRect;
@property (nonatomic, assign) int cropX;
@property (nonatomic, assign) int cropY;
@property (nonatomic, assign) int cropWidth;
@property (nonatomic, assign) int cropHeight;
- (id)initWithTexture:(SPTexture *)texture;
- (id)initWithContentsOfFile:(NSString *)path;
+ (SXCroppedImage *)imageWithTexture:(SPTexture *)texture;
+ (SXCroppedImage *)imageWithContentsOfFile:(NSString *)path;

Implementation (SXCroppedImage.m)

#import "SXCroppedImage.h"
#import "SPRenderSupport.h"
#import "SPTexture.h"
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
@implementation SXCroppedImage
@synthesize cropX = mCropX;
@synthesize cropY = mCropY;
@synthesize cropWidth = mCropW;
@synthesize cropHeight = mCropH;
+ (SXCroppedImage *)imageWithTexture:(SPTexture *)texture {
    return [[[SXCroppedImage alloc] initWithTexture:texture] autorelease];
+ (SXCroppedImage *)imageWithContentsOfFile:(NSString *)path {
    return [[[SXCroppedImage alloc] initWithContentsOfFile:path] autorelease];
- (id)initWithTexture:(SPTexture *)texture {
    if (self = [super initWithTexture:texture]) {
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        mScreenHeightPixels = screenSize.height * [SPStage contentScaleFactor];
        [self setCropRect:CGRectMake(0,0,screenSize.width,screenSize.height)];
    return self;
- (id)initWithContentsOfFile:(NSString *)path {
    return [self initWithTexture:[SPTexture textureWithContentsOfFile:path]];
- (void)render:(SPRenderSupport *)support {    
    glScissor(mCropXPixels, mCropYPixelsFlipped, mCropWPixels, mCropHPixels);
    [super render:support];
- (CGRect)cropRect {
    return CGRectMake(mCropX, mCropY, mCropW, mCropH);
- (void)setCropRect:(CGRect)rect {
    mCropX = rect.origin.x;
    mCropY = rect.origin.y;
    mCropW = rect.size.width;
    mCropH = rect.size.height;
    mCropXPixels = mCropX * [SPStage contentScaleFactor];
    mCropWPixels = mCropW * [SPStage contentScaleFactor];
    mCropHPixels = mCropH * [SPStage contentScaleFactor];
    mCropYPixelsFlipped = mScreenHeightPixels - (mCropY * [SPStage contentScaleFactor]) - mCropHPixels;


  • 2011/03/24 17:00 PDT: First public version
  • 2011/03/24 23:00 PDT: Updated by Shilo
  • 2011/03/25 17:20 PDT: Updated by Ron to support Retina Display


No comments so far. Feel free to edit this part of the page.

  extensions/sxcroppedimage.txt · Last modified: 2015/09/14 11:14 (external edit)
Powered by DokuWiki