首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

iOS 应用UIBezierPath类实现随手画画板

2014-04-18 
iOS使用UIBezierPath类实现随手画画板在上一篇文章中我介绍了 UIBezierPath类 介绍 ,下面这篇文章介绍一下

iOS 使用UIBezierPath类实现随手画画板

在上一篇文章中我介绍了 UIBezierPath类 介绍 ,下面这篇文章介绍一下如何通过这个类实现一个简单的随手画画板的简单程序demo,功能包括:划线(可以调整线条粗细,颜色),撤销笔画,回撤笔画,清除画布,橡皮擦。当然也可以扩展其他的功能。

iOS  应用UIBezierPath类实现随手画画板


一、首先看看实现划线部分的关键代码吧!



这里的处理是用到一个弹出框,点击可以选择颜色。

下面讲一下如何实现这个颜色选择器。其中参考自:点击打开链接

实现原理:弹出框中显示的是一张图片,我们通过一个函数处理,获取到这个图片的所有像素点的透明度和RGB(每一个值占1Byte)数据(是一个数组),然后通过点击获取到点的坐标,就可以获取到这个像素点的透明度和RGB值了。

实现的有关代码如下:

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {UITouch* touch = [touches anyObject];CGPoint point = [touch locationInView:self.imgView]; //where image was tappedlastColor = [self getPixelColorAtLocation:point];[pickedColorDelegate pickedColor:(UIColor*)lastColor];}// Please refer to iOS Developer Library for more details regarding the following two methods- (UIColor*) getPixelColorAtLocation:(CGPoint)point {UIColor* color = nil;CGImageRef inImage = self.imgView.image.CGImage;// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue    CGContextRef contexRef = [self createARGBBitmapContext:inImage];if (contexRef == NULL) { return nil; /* error */ }    size_t w = CGImageGetWidth(inImage);// problem!size_t 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(contexRef, rect, inImage);// Now we can get a pointer to the image data associated with the bitmap// context.unsigned char* data = CGBitmapContextGetData (contexRef);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))+round(point.x)); //这是一个二维数组,offset是确定数组下标int alpha =  data[offset];int red = data[offset+1];int green = data[offset+2];int blue = data[offset+3];NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];}// When finished, release the contextCGContextRelease(contexRef);// Free image data memory for the contextif (data) { free(data); }return color;}- (CGContextRef)  createARGBBitmapContext:(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);  //deprecatedcolorSpace = 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 returningCGColorSpaceRelease( colorSpace );return context;}



热点排行