Last active
December 19, 2015 10:29
-
-
Save Shilo/5941312 to your computer and use it in GitHub Desktop.
A category for Sparrow 2.X that allows a SPDisplayObject to be dynamically converted to a UIImage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SPDisplayObject+Image.h | |
// Sparrow 2.X | |
// | |
// Created by Shilo White on 7/6/13. | |
// | |
#import "SPDisplayObject.h" | |
@interface SPDisplayObject (Image) | |
@property (nonatomic, readonly) UIImage *UIImage; | |
@property (nonatomic, readonly) UIImage *fullSizeUIImage; | |
- (UIImage *)UIImageWithBackgroundColor:(uint)backgroundColor; | |
- (UIImage *)UIImageWithBackgroundColor:(uint)backgroundColor fullSize:(BOOL)fullSize; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SPDisplayObject+Image.m | |
// Sparrow 2.X | |
// | |
// Created by Shilo White on 7/6/13. | |
// | |
#import "SPDisplayObject+Image.h" | |
#import "SPRenderSupport.h" | |
#import "SPViewController.h" | |
@interface SPViewController (RenderSupport) | |
@property (nonatomic, readonly) SPRenderSupport *renderSupport; | |
@end | |
@interface SPViewController () { | |
SPRenderSupport *_support; | |
} | |
@end | |
@implementation SPViewController (RenderSupport) | |
- (SPRenderSupport *)renderSupport { | |
return _support; | |
} | |
@end | |
@implementation SPDisplayObject (Image) | |
- (UIImage *)UIImage { | |
return [self UIImageWithBackgroundColor:0x0]; | |
} | |
- (UIImage *)fullSizeUIImage { | |
return [self UIImageWithBackgroundColor:0x0 fullSize:YES]; | |
} | |
- (UIImage *)UIImageWithBackgroundColor:(uint)backgroundColor { | |
return [self UIImageWithBackgroundColor:backgroundColor fullSize:NO]; | |
} | |
- (UIImage *)UIImageWithBackgroundColor:(uint)backgroundColor fullSize:(BOOL)fullSize { | |
SPViewController *viewController = Sparrow.currentController; | |
int contentScaleFactor = Sparrow.contentScaleFactor; | |
int width = self.width*contentScaleFactor; | |
int height = self.height*contentScaleFactor; | |
int bufferLength = width*height*4; | |
unsigned char buffer[bufferLength]; | |
[SPRenderSupport clearWithColor:backgroundColor alpha:1.0f]; | |
[self render:viewController.renderSupport]; | |
glReadPixels(0, (Sparrow.stage.height*contentScaleFactor)-height, width, height, GL_RGBA, GL_UNSIGNED_BYTE, &buffer); | |
CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(NULL, &buffer, bufferLength, NULL); | |
CGImageRef imageRef = CGImageCreate(width, height, 8, 32, width*4, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, dataProviderRef, NULL, true, kCGRenderingIntentDefault); | |
if (!fullSize) { | |
width /= contentScaleFactor; | |
height /= contentScaleFactor; | |
} | |
uint32_t *pixels = (uint32_t *)malloc(bufferLength); | |
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width*4, CGImageGetColorSpace(imageRef), kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big); | |
CGContextTranslateCTM(context, 0, height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); | |
CGImageRelease(imageRef); | |
imageRef = CGBitmapContextCreateImage(context); | |
UIImage *image = [UIImage imageWithCGImage:imageRef]; | |
CGContextRelease(context); | |
free(pixels); | |
CGImageRelease(imageRef); | |
CGDataProviderRelease(dataProviderRef); | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment