美文网首页
iOS获取手指点击背景图的坐标的颜色(亲测,不需要关心UIIma

iOS获取手指点击背景图的坐标的颜色(亲测,不需要关心UIIma

作者: boxKUn | 来源:发表于2018-01-23 17:05 被阅读186次

公司最近要做一个拾色器,就是自己手动选择一张图片,获取手指点击的哪一个点的颜色,然后跳转其他页面,做颜色调试.

思考的问题:

1.刚接到这需求的时候,在网上搜了一下方法,获取点击点的颜色方法很多,但是都有一个弊端,就是一定要求,UIImageView的frame一定要跟图片一样,才能准确获取点得色彩.

2.因为还需要对图片做缩放的功能,考虑到可以把图片放到scrollView和在图片上添加手势两种方法(鉴于没有经验,只好都尝试一遍,发现scrollView是完美的)

3.获取手指点击的坐标,一开始通过添加手势来缩放,可以直接调用系统TouchBegin方法获取,但是将imageView放在scrollView中,次方法获取不到坐标点,只能对imageView添加一个单点击手势.

实现思路:

1.获取需要展示的图片,并且拿到款和高,添加一个bgImageView,frame设置为图片大小,直接添加到当前控制器View上.

2.添加一个scrollView到控制器View上,frame设置为view.bounds.

3.获取需要展示的图片,并且拿到图片的宽和高.然后除以当前屏幕宽和高,获取一个比例scale.添加一个imageView到scrollView上,frame是经过比例缩放的.

4.添加一个单击手势给imageView,实现对应的方法通过手势方法获取点:CGPoint point = [sender locationInView:_imageView].

5.在获取色调的方法中,传进去的image是bgImageView的image属性,坐标点是乘以刚刚第二步计算的出来的scale,这样,点击屏幕的点就可以准确获取到图片对应点得颜色.

贴出获取色彩的代码:

- (UIColor*)getPixelColorAtLocation:(CGPoint)point  

{  UIColor* color = [UIColor whiteColor];  

[self.colorArr removeAllObjects];  

if (point.x < self.imageView.frame.size.width && point.x > 0 && point.y < self.imageView.frame.size.height && point.y > 0) {  UIImageView *colorImageView=self.backgroundImageV;  CGImageRef inImage = colorImageView.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 cgctx = [self createARGBBitmapContextFromImage:inImage];  

if (cgctx == NULL)  

        {  

return nil;  

        }  

size_t w = CGImageGetWidth(inImage);  

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

// Now we can get a pointer to the image data associated with the bitmap  

// context.  

unsignedchar* data = CGBitmapContextGetData (cgctx);  

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.  

@try  

            {  

int offset = 4*((w*round(point.y * self.scale))+round(point.x * self.scale));  

//NSLog(@"offset: %d", 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);  

NSLog(@"%d%d%d",red,green,blue);  

[self.colorArr addObject:@(alpha / 255.0f)];  

[self.colorArr addObject:@(red)];  

[self.colorArr addObject:@(green)];  

[self.colorArr addObject:@(blue)];  

color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];             }  

@catch (NSException * e)  

            {  

NSLog(@"%@",[e reason]);  

            }  

@finally  

            { 

            } 

        } 

// When finished, release the context 

        CGContextRelease(cgctx); 

// Free image data memory for the context 

if (data)  

        {  

            free(data);  

        } 

    }  

self.pickView.backgroundColor = color;  

return color; 

- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {  

CGContextRef    context =NULL;

   CGColorSpaceRef colorSpace; 

voidvoid *          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   = (int)(pixelsWide * 4); 

bitmapByteCount     =(int)(bitmapBytesPerRow * pixelsHigh); 

// Use the generic RGB color space. 

    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; 

}  

到此,此功能需求完美实现,需要源码的小伙伴,可以留言交流~

相关文章

网友评论

      本文标题:iOS获取手指点击背景图的坐标的颜色(亲测,不需要关心UIIma

      本文链接:https://www.haomeiwen.com/subject/ggrqjttx.html