我最近一直在使用核心图形编写大量的图像处理代码,我已经制作了很多能够操作颜色、应用混合、模糊等效果的工作滤镜。但是,我在编写将点彩效果应用于图像的滤镜时遇到了问题,就像这样:
我正在尝试获取像素的颜色并用该颜色填充椭圆形,通过循环遍历图像,并每隔几个像素执行此操作,以下是我的代码:
编辑:这是我的新代码,这次它只在图像底部绘制了几个小圆圈,我是否按照您所说的方式正确执行了?
我正在尝试获取像素的颜色并用该颜色填充椭圆形,通过循环遍历图像,并每隔几个像素执行此操作,以下是我的代码:
编辑:这是我的新代码,这次它只在图像底部绘制了几个小圆圈,我是否按照您所说的方式正确执行了?
-(UIImage*)applyFilterWithAmount:(double)amount {
CGImageRef inImage = self.CGImage;
CFDataRef m_dataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8* m_pixelBuf = (UInt8*)CFDataGetBytePtr(m_dataRef);
int length = CFDataGetLength(m_dataRef);
CGContextRef ctx = CGBitmapContextCreate(m_pixelBuf,
CGImageGetWidth(inImage),
CGImageGetHeight(inImage),
CGImageGetBitsPerComponent(inImage),
CGImageGetBytesPerRow(inImage),
CGImageGetColorSpace(inImage),
CGImageGetBitmapInfo(inImage));
int row = 0;
int imageWidth = self.size.width;
if ((row%imageWidth)==0) {
row++;
}
int col = row%imageWidth;
for (int i = 0; i<length; i+=4) {
//filterPointillize(m_pixelBuf, i, context);
int r = i;
int g = i+1;
int b = i+2;
int red = m_pixelBuf[r];
int green = m_pixelBuf[g];
int blue = m_pixelBuf[b];
CGContextSetRGBFillColor(ctx, red/255, green/255, blue/255, 1.0);
CGContextFillEllipseInRect(ctx, CGRectMake(col, row, amount, amount));
}
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage* finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CFRelease(m_dataRef);
return finalImage;
}