HardBirch

【IOS】仿 AppleTree 画板中的颜色选择器 (半成品)

时间:12-05-07 栏目:iOS游戏开发技术 作者:张飞不张,文采横飞 评论:4 点击: 2,502 次

 

花了两个晚上的时间,给大家带来一个颜色选择器。画图的部分用数组记录点,来绘制,感觉不是很流畅,希望有涂鸦类,或者小画板开发经验的童鞋透露点心得。

效果图: 类似左边的这样的

//
//  ColorPicker.h
//  Draw
//
//  Created by  on 12-5-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "Canvas.h"
@interface ColorPicker : UIView
{
        CGContextRef contextref;
        UIImage * colorbar;
        UIImage * glass;
        UIColor * drawColor;
        CGPoint offsetPoint;
        unsigned char* data;
        size_t w;
        size_t h;
        Canvas *canvas;
}
@property (nonatomic,retain) UIImage *colorbar;
@property (nonatomic,retain) UIImage *glass;
@property (nonatomic,assign) CGContextRef contextref;
@property (nonatomic,assign) CGPoint offsetPoint;
@property (nonatomic,assign) UIColor * drawColor;
@property (nonatomic,assign) Canvas *canvas;
- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage;
- (UIColor*) getPixelColorAtLocation:(CGPoint)point;
- (UIColor*) setGlassPoint:(CGPoint) point;
- (void) getData;
@end

 

//
//  ColorPicker.m
//  Draw
//
//  Created by  on 12-5-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ColorPicker.h"

@implementation ColorPicker

@synthesize colorbar,glass,contextref,offsetPoint,drawColor,canvas;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.colorbar = [UIImage imageNamed:@"colorbar.png"];
        self.layer.cornerRadius = frame.size.width/2;
        self.layer.masksToBounds = YES;
        drawColor = [UIColor brownColor];
        contextref = NULL;
        data = NULL;
        offsetPoint = CGPointMake(0, 0);
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    drawColor = [self getPixelColorAtLocation:CGPointMake(5, offsetPoint.y)];
    [canvas setCurrentColor:drawColor];
    CGContextSetFillColorWithColor(context,drawColor.CGColor);
	CGContextFillRect(context, rect);
    CGContextSaveGState(context);

}

-(UIColor *) setGlassPoint:(CGPoint) point
{
   offsetPoint = CGPointMake(5, point.y-130);
   [self setNeedsDisplay];
    return drawColor;
}

// Please refer to iOS Developer Library for more details regarding the following two methods
- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
	if (data == NULL) {
       [self getData];
    }
    UIColor *tmpColor;
	if (data != NULL) {
		//offset locates the pixel in the data from x,y.
		//4 for 4 bytes of data per pixel, w is width of one row of data.
		int offset = 4*((w*round(point.y)));
		int alpha =  data[offset];
		int red = data[offset+1];
		int green = data[offset+2];
		int blue = data[offset+3];
		tmpColor = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
	}

	return tmpColor;
}

- (void) getData
{
    CGImageRef inImage = self.colorbar.CGImage;
	// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
    if (contextref == NULL) {
        contextref = [self createARGBBitmapContextFromImage:inImage];
    }

	if (contextref == NULL) { return; /* error */ }

    w = CGImageGetWidth(inImage);		// problem!
    h = CGImageGetHeight(inImage);
	CGRect rect = {{0,0},{w,h}}; 

	// 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(contextref, rect, inImage); 

	// Now we can get a pointer to the image data associated with the bitmap
	// context.
    data = CGBitmapContextGetData (contextref);
}

- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

	CGContextRef    context = NULL;
	CGColorSpaceRef colorSpace;
	void *          bitmapData;
	int             bitmapByteCount;
	int             bitmapBytesPerRow;

	// Get image width, height. We'll use the entire image.
	size_t pixelsWide = CGImageGetWidth(inImage);
	size_t pixelsHigh = CGImageGetHeight(inImage);

	// 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   = (pixelsWide * 4);
	bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

	// Use the generic RGB color space.
	//colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);  //deprecated
	colorSpace = CGColorSpaceCreateDeviceRGB();
	if (colorSpace == NULL)
	{
		fprintf(stderr, "Error allocating color spacen");
		return NULL;
	}

	// 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)
	{
		fprintf (stderr, "Memory not allocated!");
		CGColorSpaceRelease( colorSpace );
		return NULL;
	}

	// 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.
	context = CGBitmapContextCreate (bitmapData,
									 pixelsWide,
									 pixelsHigh,
									 8,      // bits per component
									 bitmapBytesPerRow,
									 colorSpace,
									 kCGImageAlphaPremultipliedFirst);
	if (context == NULL)
	{
		free (bitmapData);
		fprintf (stderr, "Context not created!");
	}

	// Make sure and release colorspace before returning
	CGColorSpaceRelease( colorSpace );

	return context;
}

-(void) dealloc
{
    if(data) {free((data));}
    [colorbar release];
    [glass release];
    [super dealloc];
}
@end

 

下载地址:http://download.csdn.net/detail/toss156/4282626

【IOS】仿 AppleTree 画板中的颜色选择器 (半成品):目前有4 条留言

  1. 4楼
    ComeOnTom:

    mark….

    2012-05-07 21:44 [回复]
  2. 地板
    dongge_111:

    $_$顶…

    2012-05-09 03:06 [回复]
  3. 板凳
    wl1990910:

    不错

    2012-05-12 10:30 [回复]
  4. 沙发
    x1135768777:

    顶起

    2012-05-13 23:33 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐