【问题标题】:How to get the pixel color values of custom image inside imageview ios?如何在imageview ios中获取自定义图像的像素颜色值?
【发布时间】:2014-01-09 19:05:11
【问题描述】:

我知道以前有人问过类似的问题。

我想要的是得到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;
}

我是 ios 新手,所以请解释一下,并建议一些教程会很棒。

【问题讨论】:

  • Achievelimetless 和 Lapinou 都是正确的。

标签: ios objective-c image-processing uiimageview color-picker


【解决方案1】:

使用这个example article。它正在谈论使用图像的颜色选择器。您可以从中非常轻松地了解所需的信息。在我的应用程序中帮助了我。如果需要任何帮助/建议,请告诉我..:)

编辑:

像这样更新您的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;
}

让我知道这个修复不起作用.. :)

这是我的代码的the GitHub。使用它来实现选择器图像。如果需要更多信息,请告诉我

【讨论】:

  • 我已经点击了链接但是我得到了错误的像素值。
  • ya.. 代码中需要做一个小的编辑。文章的 cmets 中提到了所需的编辑。但是,我也会在我的答案中复制编辑。 5分钟后检查。 :)
  • 就像我在编辑之前添加了CGRect frame = self.imageViewGallery.frame;
  • 我们能否用它添加更多功能,例如在图像上移动的圆形指针和填充所选颜色的容器。
  • 是的。我已经实现了与您想要实现的完全一样的实现。但我只能在 12 小时后与您共享代码。它在我的办公室里。如果可能,请在 12 小时后在此处留言以提醒我 ..:)
【解决方案2】:

只要用这个方法,对我有用:

- (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];

【讨论】:

  • 收到 2 个警告以及应用程序崩溃的位置 1- 找不到实例方法“-createARGBBitmapContextFromImage:”(返回类型默认为“id”)。 2- 使用“id”类型的表达式初始化“CGContextRef”(又名“struct CGContext *”)的不兼容指针类型。在这一行`CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];`你能告诉我需要哪些基本方法吗?
  • 当然,只要点击@achievelimitless 的链接:markj.net/iphone-uiimage-pixel-color,这是您需要的完整代码教程;)
  • @quickApp 别担心.. 我投票支持 Lapinou。谢谢你的好手势..:)
【解决方案3】:

如果您尝试通过 CGBitmapContextGetData 从图像中获取颜色并设置,例如,在视图背景中,这将是 iPhone 6 和更高版本中的不同颜色。在 iPhone 5 中一切都会好起来的)有关此的更多信息Getting the right colors in your iOS app

这个通过 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];

}

【讨论】:

    【解决方案4】:

    您想知道如何从图像视图中获取图像吗?

    UIImageView 有一个属性,图像。只需使用该属性。

    【讨论】:

    • 不,亲爱的,例如,有一个 imageView.image 包含任何图像,可以说您当前的堆栈配置文件图片,如果我们将它传递给 imageview,那么我们应该有一些指针在它上面,当单击眼睛点时返回它的 rgb 像素值,无论它是棕色、蓝色眼睛颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2018-05-10
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2013-07-21
    相关资源
    最近更新 更多