登录站点

用户名

密码

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

已有 138 次阅读  2012-05-16 17:15   标签半成品  画板 

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

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


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

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


  1. //  
  2. //  ColorPicker.h  
  3. //  Draw  
  4. //  
  5. //  Created by  on 12-5-6.  
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import <QuartzCore/QuartzCore.h>  
  11. #import "Canvas.h"  
  12. @interface ColorPicker : UIView  
  13. {  
  14.         CGContextRef contextref;  
  15.         UIImage * colorbar;  
  16.         UIImage * glass;  
  17.         UIColor * drawColor;  
  18.         CGPoint offsetPoint;  
  19.         unsigned char* data;  
  20.         size_t w;     
  21.         size_t h;  
  22.         Canvas *canvas;  
  23. }     
  24. @property (nonatomic,retain) UIImage *colorbar;  
  25. @property (nonatomic,retain) UIImage *glass;  
  26. @property (nonatomic,assign) CGContextRef contextref;  
  27. @property (nonatomic,assign) CGPoint offsetPoint;  
  28. @property (nonatomic,assign) UIColor * drawColor;  
  29. @property (nonatomic,assign) Canvas *canvas;  
  30. - (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage;  
  31. - (UIColor*) getPixelColorAtLocation:(CGPoint)point;  
  32. - (UIColor*) setGlassPoint:(CGPoint) point;  
  33. - (void) getData;  
  34. @end  

  1. //  
  2. //  ColorPicker.m  
  3. //  Draw  
  4. //  
  5. //  Created by  on 12-5-6.  
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import "ColorPicker.h"  
  10.   
  11. @implementation ColorPicker  
  12.   
  13. @synthesize colorbar,glass,contextref,offsetPoint,drawColor,canvas;  
  14. - (id)initWithFrame:(CGRect)frame  
  15. {  
  16.     self = [super initWithFrame:frame];  
  17.     if (self) {  
  18.         // Initialization code  
  19.         self.colorbar = [UIImage imageNamed:@"colorbar.png"];  
  20.         self.layer.cornerRadius = frame.size.width/2;  
  21.         self.layer.masksToBounds = YES;  
  22.         drawColor = [UIColor brownColor];  
  23.         contextref = NULL;  
  24.         data = NULL;  
  25.         offsetPoint = CGPointMake(0, 0);  
  26.     }  
  27.     return self;  
  28. }  
  29.   
  30.   
  31. // Only override drawRect: if you perform custom drawing.  
  32. // An empty implementation adversely affects performance during animation.  
  33. - (void)drawRect:(CGRect)rect  
  34. {  
  35.     // Drawing code  
  36.     CGContextRef context = UIGraphicsGetCurrentContext();  
  37.     drawColor = [self getPixelColorAtLocation:CGPointMake(5, offsetPoint.y)];  
  38.     [canvas setCurrentColor:drawColor];  
  39.     CGContextSetFillColorWithColor(context,drawColor.CGColor);  
  40.     CGContextFillRect(context, rect);  
  41.     CGContextSaveGState(context);  
  42.      
  43. }  
  44.   
  45. -(UIColor *) setGlassPoint:(CGPoint) point  
  46. {     
  47.    offsetPoint = CGPointMake(5, point.y-130);  
  48.    [self setNeedsDisplay];  
  49.     return drawColor;  
  50. }  
  51.   
  52.   
  53. // Please refer to iOS Developer Library for more details regarding the following two methods  
  54. - (UIColor*) getPixelColorAtLocation:(CGPoint)point {  
  55.     if (data == NULL) {  
  56.        [self getData];  
  57.     }  
  58.     UIColor *tmpColor;  
  59.     if (data != NULL) {  
  60.         //offset locates the pixel in the data from x,y.   
  61.         //4 for 4 bytes of data per pixel, w is width of one row of data.  
  62.         int offset = 4*((w*round(point.y)));  
  63.         int alpha =  data[offset];   
  64.         int red = data[offset+1];   
  65.         int green = data[offset+2];   
  66.         int blue = data[offset+3];   
  67.         tmpColor = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];  
  68.     }  
  69.       
  70.     return tmpColor;  
  71. }  
  72.   
  73. - (void) getData  
  74. {  
  75.     CGImageRef inImage = self.colorbar.CGImage;  
  76.     // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue  
  77.     if (contextref == NULL) {  
  78.         contextref = [self createARGBBitmapContextFromImage:inImage];  
  79.     }  
  80.       
  81.     if (contextref == NULL) { return/* error */ }  
  82.       
  83.     w = CGImageGetWidth(inImage);       // problem!  
  84.     h = CGImageGetHeight(inImage);  
  85.     CGRect rect = {{0,0},{w,h}};   
  86.       
  87.     // Draw the image to the bitmap context. Once we draw, the memory   
  88.     // allocated for the context for rendering will then contain the   
  89.     // raw image data in the specified color space.  
  90.     CGContextDrawImage(contextref, rect, inImage);   
  91.       
  92.     // Now we can get a pointer to the image data associated with the bitmap  
  93.     // context.  
  94.     data = CGBitmapContextGetData (contextref);  
  95. }  
  96.   
  97. - (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {  
  98.       
  99.     CGContextRef    context = NULL;  
  100.     CGColorSpaceRef colorSpace;  
  101.     void *          bitmapData;  
  102.     int             bitmapByteCount;  
  103.     int             bitmapBytesPerRow;  
  104.       
  105.     // Get image width, height. We'll use the entire image.  
  106.     size_t pixelsWide = CGImageGetWidth(inImage);  
  107.     size_t pixelsHigh = CGImageGetHeight(inImage);  
  108.       
  109.     // Declare the number of bytes per row. Each pixel in the bitmap in this  
  110.     // example is represented by 4 bytes; 8 bits each of red, green, blue, and  
  111.     // alpha.  
  112.     bitmapBytesPerRow   = (pixelsWide * 4);  
  113.     bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);  
  114.       
  115.     // Use the generic RGB color space.  
  116.     //colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);  //deprecated  
  117.     colorSpace = CGColorSpaceCreateDeviceRGB();  
  118.     if (colorSpace == NULL)  
  119.     {  
  120.         fprintf(stderr, "Error allocating color space\n");  
  121.         return NULL;  
  122.     }  
  123.       
  124.     // Allocate memory for image data. This is the destination in memory  
  125.     // where any drawing to the bitmap context will be rendered.  
  126.     bitmapData = malloc( bitmapByteCount );  
  127.     if (bitmapData == NULL)   
  128.     {  
  129.         fprintf (stderr, "Memory not allocated!");  
  130.         CGColorSpaceRelease( colorSpace );  
  131.         return NULL;  
  132.     }  
  133.       
  134.     // Create the bitmap context. We want pre-multiplied ARGB, 8-bits   
  135.     // per component. Regardless of what the source image format is   
  136.     // (CMYK, Grayscale, and so on) it will be converted over to the format  
  137.     // specified here by CGBitmapContextCreate.  
  138.     context = CGBitmapContextCreate (bitmapData,  
  139.                                      pixelsWide,  
  140.                                      pixelsHigh,  
  141.                                      8,      // bits per component  
  142.                                      bitmapBytesPerRow,  
  143.                                      colorSpace,  
  144.                                      kCGImageAlphaPremultipliedFirst);  
  145.     if (context == NULL)  
  146.     {  
  147.         free (bitmapData);  
  148.         fprintf (stderr, "Context not created!");  
  149.     }  
  150.       
  151.     // Make sure and release colorspace before returning  
  152.     CGColorSpaceRelease( colorSpace );  
  153.       
  154.      
  155.     return context;  
  156. }  
  157.   
  158. -(void) dealloc  
  159. {  
  160.     if(data) {free((data));}  
  161.     [colorbar release];  
  162.     [glass release];  
  163.     [super dealloc];  
  164. }  
  165. @end  

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

上一篇: C语言深入浅出 :回味经典 下一篇: android 多媒体和相机详解一

分享 举报