Skip to content

Commit

Permalink
Merge pull request #7 from larsacus/master
Browse files Browse the repository at this point in the history
Fix inactive bug & performance enhancements
  • Loading branch information
luisespinoza committed Nov 2, 2013
2 parents 8710fd4 + b2de4f5 commit b30a757
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
1 change: 0 additions & 1 deletion LEColorPicker/LEColorPicker.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
CAEAGLLayer* _eaglLayer;
EAGLContext *_context;
dispatch_queue_t taskQueue;
BOOL _isWorking;
}
/**
This instance method allows the client object to generate three colors from a specific UIImage. This method generate synchronously colors for background, primary and secondary colors, encapsulated in a LEColorScheme object.
Expand Down
84 changes: 44 additions & 40 deletions LEColorPicker/LEColorPicker.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ - (id)init
if (self) {
// Create queue and set working flag initial state
taskQueue = dispatch_queue_create("LEColorPickerQueue", DISPATCH_QUEUE_SERIAL);
_isWorking = NO;
dispatch_set_target_queue(taskQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0));

// Add notifications for multitasking and background aware
[self addNotificationObservers];
Expand All @@ -174,17 +174,15 @@ - (id)init
- (void)pickColorsFromImage:(UIImage *)image
onComplete:(void (^)(LEColorScheme *colorsPickedDictionary))completeBlock
{
if (!_isWorking && [self isAppActive]) {
if ([self isAppActive]) {
dispatch_async(taskQueue, ^{
// Color calculation process
_isWorking = YES;
LEColorScheme *colorScheme = [self colorSchemeFromImage:image];

// Call complete block and pass colors result
dispatch_async(dispatch_get_main_queue(), ^{
completeBlock(colorScheme);
});
_isWorking = NO;
});
}
}
Expand Down Expand Up @@ -221,13 +219,25 @@ - (LEColorScheme*)colorSchemeFromImage:(UIImage*)inputImage
#pragma mark - Old interface implementation
+ (void)pickColorFromImage:(UIImage *)image onComplete:(void (^)(NSDictionary *))completeBlock
{
LEColorPicker *colorPicker = [[LEColorPicker alloc] init];
static LEColorPicker *colorPicker;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
colorPicker = [[LEColorPicker alloc] init];
});

[colorPicker pickColorsFromImage:image onComplete:^(LEColorScheme *colorScheme) {
NSDictionary *colorsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
colorScheme.backgroundColor,@"BackgroundColor",
colorScheme.primaryTextColor,@"PrimaryTextColor",
colorScheme.secondaryTextColor,@"SecondaryTextColor", nil];
completeBlock(colorsDictionary);
if ([NSThread isMainThread]) {
completeBlock(colorsDictionary);
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
completeBlock(colorsDictionary);
});
}
}];
}

Expand All @@ -237,17 +247,17 @@ - (void)setupOpenGL
{
// Start openGLES

[self setupContext];

[self setupFrameBuffer];

[self setupRenderBuffer];

[self setupDepthBuffer];

[self setupOpenGLForDominantColor];

[self setupVBOs];
if([self setupContext]){
[self setupFrameBuffer];
[self setupRenderBuffer];
[self setupDepthBuffer];
[self setupOpenGLForDominantColor];
[self setupVBOs];
}
}

- (void)renderDominant
Expand Down Expand Up @@ -281,15 +291,18 @@ - (GLuint)setupTextureFromImage:(UIImage*)image

if (!inputTextureImage) {
LELog(@"Failed to load image for texture");
exit(1);
}

size_t width = CGImageGetWidth(inputTextureImage);
size_t height = CGImageGetHeight(inputTextureImage);

GLubyte *inputTextureData = (GLubyte*)calloc(width*height*4, sizeof(GLubyte));
CGColorSpaceRef inputTextureColorSpace = CGImageGetColorSpace(inputTextureImage);
CGContextRef inputTextureContext = CGBitmapContextCreate(inputTextureData, width, height, 8, width*4, inputTextureColorSpace , kCGImageAlphaPremultipliedLast);
CGContextRef inputTextureContext = CGBitmapContextCreate(inputTextureData,
width, height,
8, width*4,
inputTextureColorSpace,
(CGBitmapInfo)kCGImageAlphaPremultipliedLast);
//3 Draw image into the context
CGContextDrawImage(inputTextureContext, CGRectMake(0, 0, width, height),inputTextureImage);
CGContextRelease(inputTextureContext);
Expand All @@ -306,18 +319,22 @@ - (GLuint)setupTextureFromImage:(UIImage*)image
return inputTexName;
}

- (void)setupContext {
- (BOOL)setupContext {
EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES2;
_context = [[EAGLContext alloc] initWithAPI:api];
if (!_context) {
//NSLog(@"Failed to initialize OpenGLES 2.0 context");
exit(1);
if (_context == NULL) {
_context = [[EAGLContext alloc] initWithAPI:api];
if (!_context) {
NSLog(@"Failed to initialize OpenGLES 2.0 context");
return NO;
}
}

if (![EAGLContext setCurrentContext:_context]) {
//NSLog(@"Failed to set current OpenGL context");
exit(1);
NSLog(@"Failed to set current OpenGL context");
return NO;
}

return YES;
}

- (void)setupFrameBuffer {
Expand Down Expand Up @@ -817,11 +834,6 @@ - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
- (void)addNotificationObservers
{
// Add observers for notification to respond at app state changes.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appWillResignActive)
name:UIApplicationWillResignActiveNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
Expand All @@ -834,22 +846,14 @@ - (void)addNotificationObservers

- (void)dealloc {
//Remove all observers
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)appWillResignActive
{
dispatch_suspend(taskQueue);
glFinish();
}

- (void)appDidEnterBackground
{
dispatch_suspend(taskQueue);
glFinish();

}

- (void)appDidEnterForeground
Expand All @@ -860,7 +864,7 @@ - (void)appDidEnterForeground
- (BOOL)isAppActive
{
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
if (state == UIApplicationStateBackground)
{
return NO;
}
Expand Down

0 comments on commit b30a757

Please sign in to comment.