图表颜色接纳器,获取图片某一点的水彩对象

亚洲城ca88手机版官网

CGFloat blue = (CGFloat)pixelData[2] / 255.0f;

@end

感谢作者:GitHub:
https://github.com/Zws-China/WSColorPicker

– (UIColor *)colorAtPixel:(CGPoint)point {

unsignedcharpixelData[4] = {0,0,0,0};

主要是拖动范围的设置

CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);

}

百度网盘链接: 密码: bk9p

}

NSUInteger height =self.size.height;

先看效果把

}

CGImageRef cgImage =self.CGImage;

重写了图片touche事件

NSInteger pointY = trunc(point.y);

CGFloat blue= (CGFloat)pixelData[2] /255.0f;

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

UITouch *touch = [touches anyObject];

CGPoint pointL = [touch locationInView:self];

if (pow(pointL.x – self.bounds.size.width/2,
2)+pow(pointL.y-self.bounds.size.width/2, 2) <=
pow(self.bounds.size.width/2, 2)) {

UIColor *color = [self colorAtPixel:pointL];

self.panView.center = pointL;

self.panView.backgroundColor =color;

if (self.currentColorBlock) {

self.currentColorBlock(color);

}}}

– (void)touchesMoved:(NSSet*)touches withEvent:(nullable UIEvent
*)event {

UITouch *touch = [touches anyObject];

CGPoint pointL = [touch locationInView:self];

if (pow(pointL.x – self.bounds.size.width/2,
2)+pow(pointL.y-self.bounds.size.width/2, 2) <=
pow(self.bounds.size.width/2, 2)) {

UIColor *color = [self getPointColorWithImage:self.image
location:pointL];

self.panView.center = pointL;

self.panView.backgroundColor =color;

if (self.currentColorBlock) {

self.currentColorBlock(color);}}}

– (void)touchesEnded:(NSSet*)touches withEvent:(nullable UIEvent
*)event {

UITouch *touch = [touches anyObject];

CGPoint pointL = [touch locationInView:self];

if (pow(pointL.x – self.bounds.size.width/2,
2)+pow(pointL.y-self.bounds.size.width/2, 2) <=
pow(self.bounds.size.width/2, 2)) {

UIColor *color = [self getPointColorWithImage:self.image
location:pointL];

self.panView.center = pointL;

self.panView.backgroundColor =color;

if (self.currentColorBlock) {

self.currentColorBlock(color);

}}}

//获取图片上某坐标点对应的像素的rgba值

– (UIColor *)getPointColorWithImage:(UIImage *)image
location:(CGPoint)point{

UIColor *pointColor = nil;

//如果图片上不存在该点返回nil

if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width,
image.size.height), point)) {

return nil;

}

NSInteger pointX = truncl(point.x); //直接舍去小数,如1.58 -> 1.0

NSInteger pointY= truncl(point.y);

CGImageRef cgImage = image.CGImage;

NSUInteger width = image.size.width;
NSUInteger height = image.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
//bitmap上下文使用的颜色空间
int bytesPerPixel = 4;  //bitmap在内存中所占的比特数
int bytesPerRow = bytesPerPixel * 1; 
//bitmap的每一行在内存所占的比特数
NSUInteger bitsPerComponent = 8; 
//内存中像素的每个组件的位数.例如,对于32位像素格式和RGB
颜色空间,你应该将这个值设为8.
unsigned char pixelData[4] = {0, 0, 0, 0};  //初始化像素信息
//创建位图文件环境。位图文件可自行百度 bitmap
CGContextRef context = CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
//指定bitmap是否包含alpha通道,像素中alpha通道的相对位置,像素组件是整形还是浮点型等信息的字符串。
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);
//当一个颜色覆盖上另外一个颜色,两个颜色的混合方式
CGContextTranslateCTM(context, -pointX, pointY – (CGFloat)height); 
//改变画布位置
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width,
(CGFloat)height ), cgImage);  //绘制图片
CGContextRelease(context);
CGFloat red = (CGFloat)pixelData[0] / 255.0f;
CGFloat green = (CGFloat)pixelData[1] / 255.0f;
CGFloat blue = (CGFloat)pixelData[2] / 255.0f;
CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
pointColor = [UIColor colorWithRed:red green:green blue:blue
alpha:alpha];
return pointColor;}

– (void)setImage:(UIImage *)image {

UIImage *temp = [self imageForResizeWithImage:image
resize:CGSizeMake(self.frame.size.width, self.frame.size.width)];

[super setImage:temp];

}

– (UIImage *)imageForResizeWithImage:(UIImage *)picture
resize:(CGSize)resize {

CGSize imageSize = resize; //CGSizeMake(25, 25)

UIGraphicsBeginImageContextWithOptions(imageSize, NO,0.0);

CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width,
imageSize.height);

[picture drawInRect:imageRect];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

return image;

}

NSUInteger height = self.size.height;

if(!CGRectContainsPoint(CGRectMake(0.0f,0.0f,self.size.width,self.size.height),
point)) {

CGContextRelease(context);

// Cancel if point is outside image coordinates

NSUInteger bitsPerComponent = 8;

bitsPerComponent,

int bytesPerRow = bytesPerPixel * 1;

@interfaceUIImage (ColorAtPixel)

CGFloat green = (CGFloat)pixelData[1] / 255.0f;

CGContextRef context = CGBitmapContextCreate(pixelData,

#import

UIImage+ColorAtPixel.h

#import 

/*
 A category on UIImage that enables you to query the color value of arbitrary 
 pixels of the image.
 */
@interface UIImage (ColorAtPixel)

- (UIColor *)colorAtPixel:(CGPoint)point;

@end


#import 

#import "UIImage+ColorAtPixel.h"


@implementation UIImage (ColorAtPixel)

- (UIColor *)colorAtPixel:(CGPoint)point {
    // Cancel if point is outside image coordinates
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
        return nil;
    }

    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = self.CGImage;
    NSUInteger width = self.size.width;
    NSUInteger height = self.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData, 
                                                 1,
                                                 1,
                                                 bitsPerComponent, 
                                                 bytesPerRow, 
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);

    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);

    // Convert color values [0..255] to floats [0.0..1.0]
    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

@end

colorSpace,

CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;

// Draw the pixel we are interested in onto the bitmap context

(UIColor *)colorAtPixel:(CGPoint)point { //
Cancel if point is outside image coordinates if
(!CGRectContainsPoint(CGRectMake(0.0f,0.0f,
self.size.width,self.size.height), point))…

– (UIColor*)colorAtPixel:(CGPoint)point {

1,

NSUInteger width =self.size.width;

bitsPerComponent,

CGContextRef context = CGBitmapContextCreate(pixelData,

NSInteger pointX = trunc(point.x);

NSInteger pointX = trunc(point.x);

CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width,
(CGFloat)height), cgImage);

CGFloat alpha = (CGFloat)pixelData[3] /255.0f;

// Draw the pixel we are interested in onto the bitmap context

returnnil;

int bytesPerPixel = 4;

return[UIColorcolorWithRed:redgreen:greenblue:bluealpha:alpha];

1,

CGColorSpaceRelease(colorSpace);

#import”UIImage+ColorAtPixel.h”

CGImageRef cgImage = self.CGImage;

CGColorSpaceRelease(colorSpace);

colorSpace,

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextDrawImage(context, CGRectMake(0.0f,0.0f, (CGFloat)width,
(CGFloat)height), cgImage);

if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width,
self.size.height), point)) {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGFloat red = (CGFloat)pixelData[0] / 255.0f;

NSInteger pointY = trunc(point.y);

bytesPerRow,

CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);

// Convert color values [0..255] to floats [0.0..1.0]

CGFloat red= (CGFloat)pixelData[0] /255.0f;

// Cancel if point is outside image coordinates

– (UIColor*)colorAtPixel:(CGPoint)point;

kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextRelease(context);

1,

# XPXRefresh

return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

return nil;

1,

NSUInteger width = self.size.width;

@end

unsigned char pixelData[4] = { 0, 0, 0, 0 };

CGContextSetBlendMode(context, kCGBlendModeCopy);

CGContextSetBlendMode(context, kCGBlendModeCopy);

@implementationUIImage (ColorAtPixel)

bytesPerRow,

}

intbytesPerPixel =4;

CGFloat green = (CGFloat)pixelData[1] /255.0f;

NSUInteger bitsPerComponent =8;

intbytesPerRow =bytesPerPixel*1;

// Convert color values [0..255] to floats [0.0..1.0]

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图