我有一个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];
}发布于 2012-04-26 07:08:57
我想出了解决问题的办法。此颜色为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];
}发布于 2012-04-23 06:51:34
>>>>编辑的.
#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);或者用这个参考..。
http://www.markj.net/iphone-uiimage-pixel-color/
希望,这将帮助you..enjoy
https://stackoverflow.com/questions/10275855
复制相似问题