【原创作品, 欢迎转载,转载请在明显处注明! 谢谢。
原文地址: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
发表评论 评论 (0 个评论)