linux像素点的颜色值怎么设置,iOS:基于图片像素点修改图片颜色

思考:图片是由大量的小方格组成的可见的图像,每一个小方格存储a、r、g、b用来定义颜色,所以写了一个demo,能够获取一张图片的所有像素点颜色并能进行修改。

效果如下:

570c0edbf8f2

Untitled.gif

//获取图片所有像素点颜色

+ (NSMutableArray *)getPixelColorsWithImage:(UIImage*)image

{

UInt32 * inputPixels;

// 分配内存

CGImageRef inputCGImage = [image CGImage];

NSUInteger imageWidth = CGImageGetWidth(inputCGImage);

NSUInteger imageHeight = CGImageGetHeight(inputCGImage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

NSUInteger bytesPerPixel = 4;

NSUInteger bitsPerComponent = 8;

NSUInteger inputBytesPerRow = bytesPerPixel * imageWidth;//每行字节数

inputPixels = (UInt32 *)calloc(imageHeight * imageWidth, sizeof(UInt32));//内存空间首个像素

// 创建context

CGContextRef context = CGBitmapContextCreate(inputPixels, imageWidth, imageHeight, bitsPerComponent, inputBytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);

NSMutableArray* arr = [NSMutableArray array];

// 遍历像素

for (NSUInteger j = 0; j < imageHeight; j++) {

for (NSUInteger i = 0; i < imageWidth; i++) {

UInt32 * currentPixel = inputPixels + (j * imageWidth) + i;

UInt32 color = *currentPixel;

UInt32 red,green,blue,alpha;

red=R(color);green=G(color);blue=B(color);alpha=A(color);

[arr addObject:[UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha/255.0]];

}

}

CGColorSpaceRelease(colorSpace);

CGContextRelease(context);

free(inputPixels);

return arr;

}

//更改图片像素点颜色

+ (UIImage*)changePixelColorsWithImage:(UIImage*)image colors:(NSArray*)colors toColor:(UIColor*)toColor

{

UInt32 * inputPixels;

// 分配内存

CGImageRef inputCGImage = [image CGImage];

NSUInteger imageWidth = CGImageGetWidth(inputCGImage);

NSUInteger imageHeight = CGImageGetHeight(inputCGImage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

NSUInteger bytesPerPixel = 4;

NSUInteger bitsPerComponent = 8;

NSUInteger inputBytesPerRow = bytesPerPixel * imageWidth;//每行字节数

inputPixels = (UInt32 *)calloc(imageHeight * imageWidth, sizeof(UInt32));//内存空间首个像素

// 创建context

CGContextRef context = CGBitmapContextCreate(inputPixels, imageWidth, imageHeight, bitsPerComponent, inputBytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);

// 遍历像素

for (NSUInteger j = 0; j < imageHeight; j++) {

for (NSUInteger i = 0; i < imageWidth; i++) {

UInt32 * currentPixel = inputPixels + (j * imageWidth) + i;

UInt32 color = *currentPixel;

UInt32 br = 0,red,green,blue,alpha;

red=R(color); green=G(color); blue=B(color); alpha=A(color);

[colors enumerateObjectsUsingBlock:^(UIColor* newColor, NSUInteger idx, BOOL * _Nonnull stop) {

const CGFloat *components = CGColorGetComponents(newColor.CGColor);

if(red == components[0] * 255.0 && green == components[1] * 255.0 && blue == components[2] * 255.0){

const CGFloat *components = CGColorGetComponents(toColor.CGColor);

int red = components[0] * 255.0;

int green = components[1] * 255.0;

int blue = components[2] * 255.0;

int alpha = components[3] * 255.0;

*currentPixel = RGBAMake(red,green,blue, alpha);

*stop = YES;

return ;

}

}];

}

}

//转换UIImage

CGImageRef newCGImage = CGBitmapContextCreateImage(context);

UIImage * newImage = [UIImage imageWithCGImage:newCGImage];

CGColorSpaceRelease(colorSpace);

CGContextRelease(context);

free(inputPixels);

return newImage;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值