Iphone 框模糊CGImageRef
我试图实现一个简单的框模糊,但遇到了一些问题。也就是说,它不是模糊图像,而是将每个像素转换为红色、绿色、蓝色或黑色。不确定到底发生了什么。任何帮助都将不胜感激 请注意,这段代码只是让它工作的第一步,我不担心速度。。。然而Iphone 框模糊CGImageRef,iphone,core-graphics,blur,Iphone,Core Graphics,Blur,我试图实现一个简单的框模糊,但遇到了一些问题。也就是说,它不是模糊图像,而是将每个像素转换为红色、绿色、蓝色或黑色。不确定到底发生了什么。任何帮助都将不胜感激 请注意,这段代码只是让它工作的第一步,我不担心速度。。。然而 - (CGImageRef)blur:(CGImageRef)base radius:(int)radius { CGContextRef ctx; CGImageRef imageRef = base; NSUInteger width = CGImageGetWidth(i
- (CGImageRef)blur:(CGImageRef)base radius:(int)radius {
CGContextRef ctx;
CGImageRef imageRef = base;
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);
char red = 0;
char green = 0;
char blue = 0;
for (int widthIndex = radius; widthIndex < width - radius; widthIndex++) {
for (int heightIndex = radius; heightIndex < height - radius; heightIndex++) {
red = 0;
green = 0;
blue = 0;
for (int radiusY = -radius; radiusY <= radius; ++radiusY) {
for (int radiusX = -radius; radiusX <= radius; ++radiusX) {
int xIndex = widthIndex + radiusX;
int yIndex = heightIndex + radiusY;
int index = ((yIndex * width) + xIndex) * 4;
red += rawData[index];
green += rawData[index + 1];
blue += rawData[index + 2];
}
}
int currentIndex = ((heightIndex * width) + widthIndex) * 4;
int divisor = (radius * 2) + 1;
divisor *= divisor;
int finalRed = red / divisor;
int finalGreen = green / divisor;
int finalBlue = blue / divisor;
rawData[currentIndex] = (char)finalRed;
rawData[currentIndex + 1] = (char)finalGreen;
rawData[currentIndex + 2] = (char)finalBlue;
}
}
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth( imageRef ),
CGImageGetHeight( imageRef ),
8,
CGImageGetBytesPerRow( imageRef ),
CGImageGetColorSpace( imageRef ),
kCGImageAlphaPremultipliedLast );
imageRef = CGBitmapContextCreateImage (ctx);
CGContextRelease(ctx);
free(rawData);
[(id)imageRef autorelease];
return imageRef;
}
-(CGImageRef)模糊:(CGImageRef)基半径:(int)半径{
CGContextRef-ctx;
CGImageRef imageRef=基准;
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
无符号字符*rawData=malloc(高度*宽度*4);
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSU整数比特分量=8;
CGContextRef context=CGBitmapContextCreate(原始数据、宽度、高度、,
bitsPerComponent、bytesPerRow、颜色空间、,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),imageRef);
CGContextRelease(上下文);
炭红=0;
炭绿=0;
炭蓝=0;
对于(int-widthIndex=radius;widthIndex 对于(int radiusY=-radius;radiusY而言,字符颜色应声明为int
int red = 0;
int green = 0;
int blue = 0;
我只是有一个评论。我使用了这个脚本,它真的工作得很好,速度很快。唯一的一点是,它离开的框架的半径宽度是不模糊的。所以我对它做了一些修改,使它模糊了整个区域使用镜像技术在边缘附近
为此,需要填写以下行:
// Mirroring the part between edge and blur raduis
if (xIndex<0) xIndex=-xIndex-1; else if (xIndex>width-1) xIndex=2*width-xIndex-1;
if (yIndex<0) yIndex=-yIndex-1; else if (yIndex>height-1) yIndex=2*height-yIndex-1;
然后替换for循环的标头:
for (int widthIndex = radius; widthIndex < width - radius; widthIndex++) {
for (int heightIndex = radius; heightIndex < height - radius; heightIndex++) {
for(int-widthIndex=radius;widthIndex
使用以下标题:
for (int widthIndex = 0; widthIndex < width; widthIndex++) {
for (int heightIndex = 0; heightIndex < height; heightIndex++) {
for(int-widthIndex=0;widthIndex
AH!我一贴出来就发现了。我宣布红色、绿色和蓝色为chars。应该是int,因为chars很快就被包装好了。第一个回答这个问题的人,我会接受他们的。:)
for (int widthIndex = 0; widthIndex < width; widthIndex++) {
for (int heightIndex = 0; heightIndex < height; heightIndex++) {