在 iPhone 上读取和编辑图像像素

发布于 2024-10-06 06:16:54 字数 138 浏览 1 评论 0原文

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

我想做的事情是……如果 CGPoint 与图片上的“棕色”点相交,则将半径中所有棕色像素的颜色设置为白色。更多问题还会出现,但这只是一个开始。

干杯

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?

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.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2024-10-13 06:16:54

您可以使用图片数据——二维像素数组,每个像素由一个 32 位整数表示。对于每个颜色分量(红色、绿色、蓝色和 alpga),都有一个 8 位值。 32 位整数内这些 8 位宽值的顺序随图像数据的格式而变化。关于这一切的苹果文档非常好。虽然有一些有吸引力的 Apple 东西使用 CGDataProviderCopyData 为您提供 UIImage 实际数据存储的指针,但实际上这可能会令人头痛,因为内部存储的格式从一个图像到下一个图像可能会有很大差异。在实践中,大多数进行图像处理的人似乎都使用这种方法:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文