iOS获得图片上某个点的颜色

     最近公司有需求,要将以前用的正方向色盘去除,要求换一个圆型色盘,所以我在朋友的项目中找了一些相关例子,请大家往下看。

首先在viewController.h定义一个圆的半径以及一些后续经常用到的参数:

#define Radius 100

//以下2个就不介绍了,不懂的自己查

#define ScreenWidth  [UIScreen mainScreen].bounds.size.width 

#define ScreenHeight [UIScreen mainScreen].bounds.size.height

 

@interface ViewController : UIViewController{

    UIImageView *imageView; //色盘

    UIImageView *sel;  //颜色选择器

    UIView *v; //承载色盘的视图

  UIView *currentColorView; //显示当前选择到的颜色

    UIImage *image; 

}

 

下面到.m文件下先将界面整出来,

首先在 viewDidLoad 中将承载视图初始化

UIImage  *image = [UIImage imageNamed:@"colordisk"];

v = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth-image.size.width)/2, 50, image.size.width, image.size.height)];

[self.view addSubview:v];

 

//然后将色盘添加到承载视图中

imageView = [[UIImageView alloc]initWithImage:image];

imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);

[v addSubview:imageView];

 

//颜色选择器也添加到承载视图中

UIImage *selImage = [UIImage imageNamed:@"seletcolor"];

sel = [[UIImageView alloc]initWithImage:selImage];

sel.frame = CGRectMake(0, 0, 40, 40);

sel.center = CGPointMake(imageView.frame.size.width/2, imageView.frame.size.height/2);

[v addSubview:sel];

 

//最后将当前颜色展示界面添加到主界面中

currentColorView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];

currentColorView.center = CGPointMake(ScreenWidth/2, ScreenHeight/1.5);

[currentColorView setBackgroundColor:[UIColor blackColor]];

[self.view addSubview:currentColorView];

 

没营养的已经讲解完毕,好了,上主菜

先定义一个后续要用的CGBitmapContextCreate

CGContextRef CGBitmapContextCreate(

       void *data,

       size_t width,

       size_t height,

       size_t bitsPerComponent,

       size_t bytesPerRow,

       CGColorSpaceRef colorspace,

       CGBitmapInfo bitmapInfo

);

//定义一个私有方法

//创建取点图片工作域

#pragma mark - private

- (CGContextRef)createARGBBitmapContextFromImage:(CGImageRef) inImage{

  

  CGContextRef    context = NULL;

    CGColorSpaceRef colorSpace;

    void *          bitmapData;

    int             bitmapByteCount;

    int             bitmapBytesPerRow;

//获取图片的宽度,高度

    size_t pixelsWide = CGImageGetWidth(inImage);

    size_t pixelsHigh = CGImageGetHeight(inImage);

//获取图片的大小,1个px中取4个字节数

    bitmapBytesPerRow   = (pixelsWide * 4);

    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

//使用通用的RGB颜色空间

colorSpace = CGColorSpaceCreateDeviceRGB();

    if (colorSpace == NULL)

    {

        fprintf(stderr, "创建rgb空间错误");

        return NULL;

    }

//分配图像数据内存

    bitmapData = malloc( bitmapByteCount );

    if (bitmapData == NULL)

    {

        fprintf (stderr, "Memory not allocated!");

        CGColorSpaceRelease( colorSpace );

        return NULL;

    }

//将整张图片数据化,并且放到环境空间内

    context = CGBitmapContextCreate (bitmapData,

                                     pixelsWide,

                                     pixelsHigh,

                                     8,  

                                     bitmapBytesPerRow,

                                     colorSpace,

                                     kCGImageAlphaPremultipliedFirst);

    if (context == NULL)

    {

        free (bitmapData);

        fprintf (stderr, "没有环境空间!");

    }

//释放环境空间

   CGColorSpaceRelease( colorSpace );

    //返回环境空间对象

    return context;

//以下是拿到整个数据化后的色盘图片(环境空间)我们怎么去做相关处理,最后得到我们经常使用的UIColor,我们赶紧往下看吧

 

- (UIColor*)getPixelColorAtLocation:(CGPoint)point {

    UIColor* color = nil;

    CGImageRef inImage = self.image.CGImage;

 //在这里就用到上面我们写的私有方法,这段主要就是将整个图片数据化,实现编辑图片

    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}};

    CGContextDrawImage(cgctx, rect, inImage);

    unsigned char* data = CGBitmapContextGetData (cgctx); //看到没,data就是图片

   if (data != NULL) {

        //干完活了,下面是拿“工资”的阶段,我门得到了一个很长的数组,里面都是整张图的颜色点,那么我门

  //通过传进来的CGPoint拿颜色就是了,要记得1个px 4个字节数:ARGB

        @try {

   //先数出来从数组的哪个地方开始拿

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

            //NSLog(@"offset:从下标: %d 开始拿参数", offset);

            float alpha =  data[offset];

            float red = data[offset+1];

            float green = data[offset+2];

            float blue = data[offset+3];

//拿完了我们将他们打印出来看看是不是我们要的颜色

            NSLog(@"offset: %i colors: RGB A %f %f %f  %f",offset,red,green,blue,alpha);

            NSLog(@"%f %f",point.x,point.y);

//转化成我们经常使用的UIColor

            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 {

        }

}

 

//下面是教大家怎么简单的使用上面的方法

//首先我们要得到我们想取颜色的某个点,即CGPoint

//一般来说我们都是通过touchBegan、touchMoved、touchEnded这些事件去拿到这些我们需要的CGPoint,当然还是得看需求这里就从简,使用touchBegan做例子

 

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    //获取当前手指点击的坐标

    UITouch *t = [touches anyObject];

    CGPoint p = [t locationInView:v];

//通过CGPoint得到该坐标点上的颜色,然后将“当前颜色”视图的背景颜色改成得到的颜色

    [currentColorView setBackgroundColor:[self getPixelColorAtLocation:p]];

//颜色选择器跟随手指移动

    sel.center = p;

}

 

 

 

 

 

 

 

 

 

posted @ 2015-01-24 16:21  China小伙  阅读(736)  评论(0)    收藏  举报