【问题标题】:Change color of some specific pixels in UIImage更改 UIImage 中某些特定像素的颜色
【发布时间】:2014-12-04 05:15:16
【问题描述】:

我有一个简单的 UIImageView 和一些人的图像。现在我想根据它们的位置或一些帧值来改变一些像素的颜色。如何做到这一点?

任何帮助...

【问题讨论】:

  • 在您的图像视图上添加不同颜色的视图
  • 添加另一个视图不会让我看起来很自然,我希望这个更改在原始图像中看起来很自然。
  • 使用另一个 imagview 代替视图
  • 添加图像视图或简单视图都会得到相同的结果。
  • 下面提到的答案怎么样,使用 CoreGraphics

标签: ios iphone uiimage pixel


【解决方案1】:

对于长期实施,您应该查看 Core Image Framework tutorial。 对于一次性案例,您可以在iPhone : How to change color of particular pixel of a UIImage? 参考已有的答案 我找到了很好的非 ARC 解决方案,它可以改变整个帧内的图片颜色,但您可以尝试采用它仅应用于某些像素:

- (void) grayscale:(UIImage*) image {
    CGContextRef ctx; 
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    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), imageRef);
    CGContextRelease(context);

    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel;
    for (int ii = 0 ; ii < width * height ; ++ii)
    {
       // Get color values to construct a UIColor
          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;

        rawData[byteIndex] = (char) (red);
        rawData[byteIndex+1] = (char) (green);
        rawData[byteIndex+2] = (char) (blue);

        byteIndex += 4;
    }

    ctx = CGBitmapContextCreate(rawData,  
                                CGImageGetWidth( imageRef ),  
                                CGImageGetHeight( imageRef ),  
                                8,  
                                CGImageGetBytesPerRow( imageRef ),  
                                CGImageGetColorSpace( imageRef ),  
                                kCGImageAlphaPremultipliedLast ); 

    imageRef = CGBitmapContextCreateImage (ctx);  
    UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  

    CGContextRelease(ctx);  

    self.workingImage = rawImage;  
    [self.imageView setImage:self.workingImage];

    free(rawData);

}

来源:http://brandontreb.com/image-manipulation-retrieving-and-updating-pixel-values-for-a-uiimage

【讨论】:

  • 添加了一种可以满足您需求的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 2021-08-05
  • 1970-01-01
  • 2020-02-07
相关资源
最近更新 更多