在iPhone上读取和编辑图像像素 [英] Reading and editing pixels of image on iPhone

查看:119
本文介绍了在iPhone上读取和编辑图像像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好奇如何在iPhone上读取和编辑图片的像素。我是否最好使用带有颜色的点阵列?

Curious about how to read and edit a picture's pixels on the iPhone. Am I better of using an array of points with colours?

我想做类似的事情:如果CGPoint与图片上的棕色斑点相交,设置半径为白色的所有棕色像素的颜色。

I want to do things like.. if a CGPoint intersects with a "brown" spot on the picture, set the colour of all brown pixels in a radius to white. More questions to come, but this is a start.

干杯

推荐答案

图像数据可以精确地提供给您-二维像素阵列,每个像素由32位整数表示。对于每种颜色分量(红色,绿色,蓝色和碱性),都有一个8位的值。这些32位整数中的8位宽值的顺序随图像数据的格式而变化。关于这一切的苹果文档确实很棒。尽管有一些吸引人的Apple产品使用CGDataProviderCopyData为您提供了指向UIImage实际数据存储的指针,但实际上这可能令人头疼,因为该内部存储的格式在一个图像与另一个图像之间可能有很大差异。实际上,大多数进行图像处理的人似乎都使用这种方法:

The picture data is available to you as precisely that -- a two-dimensional array of pixels, each pixel being represented by a 32 bit integer. For each of the color components (red, green, blue, and alpga) there is an 8 bit value. The ordering of these 8-bit-wide values within the 32 bit integer varies with the format of the picture data. The apple doc about all this is really good. While there is some attractive Apple stuff using CGDataProviderCopyData to give you a pointer into the actual data storage of a UIImage, in practice this can be a headache, because the format of that internal storage can vary widely from one image to the next. In practice, most people doing image processing seem to use this approach:

    CGImageRef image = [UIImage CGImage];
    NSUInteger width = CGImageGetWidth(image);
    NSUInteger height = CGImageGetHeight(image);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData_ = malloc(height * width * 4);
    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));
    CGContextRelease(context);

    //  rawData contains image data in the RGBA8888 format.

    // for any pixel at coordinate x,y -- the value is
    // 

    int pixelIndex = (bytesPerRow * y) + x * bytesPerPixel;
    unsigned char red = rawData[pixelIndex];
    green = rawData[pixelIndex + 1];
    blue = rawData[pixelIndex + 2];
    alpha = rawData[pixelIndex + 3];

这篇关于在iPhone上读取和编辑图像像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆