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

 【原创作品, 欢迎转载,转载请在明显处注明! 谢谢。    

  原文地址:http://blog.csdn.net/toss156/article/details/7542274


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

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


//
//  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 space\n");
		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

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值