Simple Color Picker Control in IPhone

June 25, 2009

ColorPicker.h
//
// ColorPicker.m
// Created by Song on 6/2/09.
// Copyright 2009 Ultie. All rights reserved.
//
#import

@interface ColorPicker : UIView {
UIView *selectedColorBox;
SEL colorChangeSelector;
id colorChangeObject;
}

@property(nonatomic) SEL colorChangeSelector;
@property(nonatomic, retain) id colorChangeObject;

-(void) onColorChange: (SEL) selector withObject: (id) object;
@end

ColorPicker.m

//
// ColorPicker.m
// Created by Song on 6/2/09.
// Copyright 2009 Ultie. All rights reserved.
//
#import "ColorPicker.h"

@implementation ColorPicker
@synthesize colorChangeSelector;
@synthesize colorChangeObject;

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
selectedColorBox = [[UIView alloc] initWithFrame: CGRectMake(0, -3, frame.size.height + 6, frame.size.height + 6)];
selectedColorBox.backgroundColor = [UIColor redColor];
[self addSubview: selectedColorBox];
}
return self;
}

- (void) fill: (float []) color withIndex: (int) i{
int colors[] = {
255, 0, 0,
255, 0, 255,
0, 0, 255,
0, 255, 255,
0, 255, 0,
255, 255, 0,
255, 0, 0
};
int colorCount = sizeof(colors) / 3 / sizeof(int);
int p = self.bounds.size.width / (colorCount - 1);
int r,g,b;
int ii = i / p;
int colorA[] = {colors[ii * 3], colors[ii * 3 + 1], colors[ii * 3 + 2]};
int colorB[] = {colors[(ii + 1) * 3], colors[(ii + 1) * 3 + 1], colors[(ii + 1) * 3 + 2]};

r = colorA[0] - (colorA[0] - colorB[0])/p * (i % p);
g = colorA[1] - (colorA[1] - colorB[1])/p * (i % p);
b = colorA[2] - (colorA[2] - colorB[2])/p * (i % p);
color[0] = r / 256.0;
color[1] = g / 256.0;
color[2] = b / 256.0;
}

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 1);
int colors[] = {
255, 0, 0,
255, 0, 255,
0, 0, 255,
0, 255, 255,
0, 255, 0,
255, 255, 0,
255, 0, 0
};
int colorCount = sizeof(colors) / 3 / sizeof(int);
int p = rect.size.width / (colorCount - 1);
for(int i = 0; i < rect.size.width; i++){
int r,g,b;
int ii = i / p;
int colorA[] = {colors[ii * 3], colors[ii * 3 + 1], colors[ii * 3 + 2]};
int colorB[] = {colors[(ii + 1) * 3], colors[(ii + 1) * 3 + 1], colors[(ii + 1) * 3 + 2]};

r = colorA[0] - (colorA[0] - colorB[0])/p * (i % p);
g = colorA[1] - (colorA[1] - colorB[1])/p * (i % p);
b = colorA[2] - (colorA[2] - colorB[2])/p * (i % p);

//NSLog(@"cc=%d p=%d i=%d ii=%d r=%d g=%d b=%d", colorCount, p, i, ii, r, g, b);
CGContextSetRGBStrokeColor(context, r/256.0, g/256.0, b/256.0, 1.0);

CGPoint addLines[] = {
CGPointMake(i, 0),
CGPointMake(i, rect.size.height)
};
CGContextAddLines(context, addLines, 2);
CGContextStrokePath(context);
}

CGContextRestoreGState(context);
}

- (void)dealloc {
[selectedColorBox release];
[colorChangeObject release];
[super dealloc];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: self];
float color[] = {0, 0, 0};
[self fill: color withIndex: point.x];
UIColor *uc = [[UIColor alloc] initWithRed: color[0] green: color[1] blue: color[2] alpha: 1];
selectedColorBox.backgroundColor = uc;
if(colorChangeObject != nil){
[colorChangeObject methodForSelector: colorChangeSelector](colorChangeObject, colorChangeSelector, uc);
}
[uc release];
selectedColorBox.center = CGPointMake(point.x, self.bounds.size.height/2);

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan: touches withEvent: event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan: touches withEvent: event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan: touches withEvent: event];
}

-(void) onColorChange: (SEL) selector withObject: (id) object{
self.colorChangeSelector = selector;
self.colorChangeObject = object;
}
@end

Categories: iphone development.

Quit IPhone Application with SDK3.0

June 25, 2009

[[UIApplication sharedApplication] terminateWithSuccess];

add below code in .h define in order to ignore compiler warnings:
@interface UIApplication (extended)
-(void) terminateWithSuccess;
@end

Categories: iphone development.

CGImage scale in iphone

June 25, 2009

[ Remember to release the return CGImageRef with CGImageRelease() manually! ]

1) scale image with scale rate.
+(CGImageRef)scaleCGImage: (CGImageRef) image withScale: (float) scale
{
// Create the bitmap context
CGContextRef context = NULL;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;

// Get image width, height. We'll use the entire image.
int width = CGImageGetWidth(image) * scale;
int height = CGImageGetHeight(image) * scale;

// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (width * 4);
bitmapByteCount = (bitmapBytesPerRow * height);

// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
return nil;
}

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
context = CGBitmapContextCreate (bitmapData,width,height,8,bitmapBytesPerRow,
colorspace,kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorspace);

if (context == NULL)
// error creating context
return nil;

// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextDrawImage(context, CGRectMake(0,0,width, height), image);

CGImageRef imgRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
free(bitmapData);

return imgRef;
}

2) Scale image with size and rotation

+(CGImageRef)scaleCGImage: (CGImageRef) image withPrefix: (CGSize) size withRotation: (float) rotation
{
CGContextRef context = NULL;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;

int width = size.width;
int height = size.height;

bitmapBytesPerRow = (width * 4);
bitmapByteCount = (bitmapBytesPerRow * height);

bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
return nil;
}
memset(bitmapData, 0, bitmapByteCount);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate (bitmapData,width,height,8,bitmapBytesPerRow,
colorspace,kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorspace);

if (context == NULL)
return nil;

if(rotation != 0){
CGAffineTransform tran = CGAffineTransformIdentity;
tran = CGAffineTransformMakeRotation(rotation);
CGContextConcatCTM(context, tran);
CGContextDrawImage(context, CGRectMake(10, 10, width - 20, height - 20), image);
}else{
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
}
CGImageRef imgRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
free(bitmapData);

return imgRef;
}

Categories: iphone development.