我有一个UIImage,我想通过设置RGBA来改变一些像素颜色。我有需要更改的所有像素的CGPoints。如何通过设置RGBA值来更改颜色?
如果无法更改UIImage像素的颜色,可以在包含图像的UIImageView上绘图,但我需要它才能很好地执行,因为我要多次执行此操作。
我该怎么做?
修改
我找到了解决问题的方法。这为CGPoint红色着色:
-(void)pixelate:(CGPoint)point
{
CGImageRef inImage = self.drawImage.image.CGImage;
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == nil) { NSAssert(NO,@"Context shouldn't be nil!"); }
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};
CGContextDrawImage(cgctx, rect, inImage);
unsigned char* data = CGBitmapContextGetData(cgctx);
if (data != nil)
{
int offset = 4*((w*round(point.y))+round(point.x));
data[offset] = 255; //alpha
data[offset+1] = 255; //red
data[offset+2] = 0; //green
data[offset+3] = 0; //blue
}
CGImageRef img = CGBitmapContextCreateImage(cgctx);
UIGraphicsEndImageContext();
CGContextRelease(cgctx);
if (data) { free(data); }
self.drawImage.image = [UIImage imageWithCGImage:img];
}
答案 0 :(得分:0)
第一个Imaport关注文件.. >>>>编辑.... 强>
#import <QuartzCore/QuartzCore.h>
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmContext = CGBitmapContextCreate(NULL, width, height, 8,bytesPerRow, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(bitmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = originalWidth, .size.height = originalHeight}, cgImage);
获取对基础像素的引用:
UInt8* data = (UInt8*)CGBitmapContextGetData(bitmContext);
像素操作:
const size_t bitmapByteCount = bytesPerRow * originalHeight;
for (size_t i = 0; i < bitmapByteCount; i += 4)
{
UInt8 a = data[i];
UInt8 r = data[i + 1];
UInt8 g = data[i + 2];
UInt8 b = data[i + 3];
data[i] = (UInt8)newAlpha
data[i + 1] = (UInt8)newRed;
data[i + 2] = (UInt8)newGreen;
data[i + 3] = (UInt8)newBlue;
}
CGImageRef newImage = CGBitmapContextCreateImage(bitmContext);
或使用此Refrence ......
http://www.markj.net/iphone-uiimage-pixel-color/
希望,这会帮助你......享受
答案 1 :(得分:0)
我找到了解决问题的方法。这为CGPoint红色着色:
-(void)pixelate:(CGPoint)point
{
CGImageRef inImage = self.drawImage.image.CGImage;
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == nil) { NSAssert(NO,@"Context shouldn't be nil!"); }
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};
CGContextDrawImage(cgctx, rect, inImage);
unsigned char* data = CGBitmapContextGetData(cgctx);
if (data != nil)
{
int offset = 4*((w*round(point.y))+round(point.x));
data[offset] = 255; //alpha
data[offset+1] = 255; //red
data[offset+2] = 0; //green
data[offset+3] = 0; //blue
}
CGImageRef img = CGBitmapContextCreateImage(cgctx);
UIGraphicsEndImageContext();
CGContextRelease(cgctx);
if (data) { free(data); }
self.drawImage.image = [UIImage imageWithCGImage:img];
}