如何在imageview ios中获取自定义图像的像素颜色值?
我知道以前也有人问过类似的问题 我想得到的是如何在imageview ios中获取自定义图像的像素颜色值?,ios,objective-c,image-processing,uiimageview,color-picker,Ios,Objective C,Image Processing,Uiimageview,Color Picker,我知道以前也有人问过类似的问题 我想得到的是Imageview中图像的RGB像素值,因此它可以是我们想要得到的任何像素值的图像 这就是我用来获取点击图像的点的方法 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; if ([touch tapCount] == 2) { //self.imageView
Imageview
中图像的RGB像素值,因此它可以是我们想要得到的任何像素值的图像
这就是我用来获取点击图像的点的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
//self.imageView.image = nil;
return;
}
CGPoint lastPoint = [touch locationInView:self.imageViewGallery];
NSLog(@"%f",lastPoint.x);
NSLog(@"%f",lastPoint.y);
}
为了得到图像,我粘贴了这个代码
+ (NSArray*)getRGBAsFromImage:(UIImage *)image atX:(int)xx andY:(int)yy count:(int)count
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
/** It requires to get the image into your data buffer, so how can we get the `ImageViewImage` **/
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
for (int ii = 0 ; ii < count ; ++ii)
{
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += 4;
UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
free(rawData);
return result;
}
+(NSArray*)getRGBAsFromImage:(UIImage*)图像atX:(int)xx安迪:(int)yy计数:(int)计数
{
NSMUTABLEARRY*result=[NSMUTABLEARRY阵列容量:计数];
/**它需要将图像放入数据缓冲区,那么我们如何才能获得“ImageViewImage”**/
CGImageRef imageRef=[图像CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
unsigned char*rawData=(unsigned char*)calloc(高度*宽度*4,sizeof(unsigned char));
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSU整数比特分量=8;
CGContextRef context=CGBitmapContextCreate(原始数据、宽度、高度、,
bitsPerComponent、bytesPerRow、颜色空间、,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),imageRef);
CGContextRelease(上下文);
//现在,您的rawData包含RGBA8888像素格式的图像数据。
int byteIndex=(bytesPerRow*yy)+xx*bytesperpoixel;
对于(int ii=0;ii
我是ios新手,所以请解释一下,并建议一些教程会很好 用这个。这是一个使用图像的颜色选择器。您可以很容易地从中了解所需信息。在我的应用程序中帮助我。如果需要任何帮助/建议,请告诉我:)
编辑:
像这样更新您的getPixelColorAtLocation:
。它会给你正确的颜色
- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
UIColor* color = nil;
CGImageRef inImage = self.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; /* error */ }
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};
/** Extra Added code for Resized Images ****/
float xscale = w / self.frame.size.width;
float yscale = h / self.frame.size.height;
point.x = point.x * xscale;
point.y = point.y * yscale;
/** ****************************************/
/** Extra Code Added for Resolution ***********/
CGFloat x = 1.0;
if ([self.image respondsToSelector:@selector(scale)]) x = self.image.scale;
/*********************************************/
// 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.
unsigned char* 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.
// int offset = 4*((w*round(point.y))+round(point.x));
int offset = 4*((w*round(point.y))+round(point.x))*x; //Replacement for Resolution
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 context
CGContextRelease(cgctx);
// Free image data memory for the context
if (data) { free(data); }
return color;
}
让我知道此修复程序不起作用:)
这是我的密码。使用它来实现选择器映像。如果需要更多信息,请告诉我。只需使用此方法,它对我有效:
- (UIColor*) getPixelColorAtLocation:(CGPoint)point
{
UIColor* color = nil;
CGImageRef inImage;
inImage = imgZoneWheel.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; /* error */ }
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.
unsigned char* 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.
int offset = 4*((w*round(point.y))+round(point.x));
alpha = data[offset];
int red = data[offset+1];
int green = data[offset+2];
int blue = data[offset+3];
color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
}
// When finished, release the context
//CGContextRelease(cgctx);
// Free image data memory for the context
if (data) { free(data); }
return color;
}
就像这样使用:
UIColor *color = [self getPixelColorAtLocation:lastPoint];
您想知道如何从图像视图中获取图像吗
UIImageView有一个属性image。只需使用该属性即可。如果您尝试通过CGBitmapContextGetData从图像中获取颜色,并设置,例如,在视图的背景中,这将与iPhone 6和更高版本中的颜色不同。在iPhone5中,一切都会好起来)更多信息 此解决方案通过UIImage为您提供正确的颜色:
- (UIColor *)getColorFromImage:(UIImage *)image pixelPoint:(CGPoint)pixelPoint {
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(pixelPoint.x, pixelPoint.y, 1.f, 1.f));
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return [UIColor colorWithPatternImage:croppedImage];
}
没有我亲爱的外观,例如有一个imageView。图像包含任何图像,比如说您当前的堆栈配置文件图片,如果我们将其传递给imageView,那么我们应该在其上有一些指针,当单击眼点时,返回该图像的rgb像素值,无论它是棕色的,蓝色眼睛。获得2个警告,应用程序崩溃的位置为1-实例方法“-createARGBBitmapContextFromImage:”未找到(返回类型默认为“id”)。2-不兼容的指针类型正在使用类型为“id”的表达式初始化“CGContextRef”(也称为“struct CGContext*”)。在这一行上,`CGContextRef cgctx=[self-createARGBBitmapContextFromImage:inImage];`你能告诉我需要哪些基本方法吗?当然,只需点击@achievelimitless:,这是你需要的完整代码的教程;)@quickApp别担心。。我投票支持拉皮努。谢谢你漂亮的手势:)我已经按照链接了,但是我得到了错误的像素值。耶。。代码中需要进行小的编辑。文章的评论中提到了所需的编辑。不过,我也会在我的答案中复制编辑。5分钟后检查。:)就像我添加的
CGRect frame=self.imageViewGallery.frame代码>在编辑之前,我们可以添加更多的功能,比如在图像上移动的圆圈指针和填充所选颜色的容器。是的。。我已经实现了你想要实现的东西。但我只能在12小时后和你分享代码。在我的办公室里。如果可能的话,请在12小时后在此留言提醒我:)Achievelimetless和Lapinou都是正确的。