Objective c 将图像转换为灰度

Objective c 将图像转换为灰度,objective-c,ios,cocoa-touch,uiview,uiimageview,Objective C,Ios,Cocoa Touch,Uiview,Uiimageview,在我的应用程序中,我有一个UIImageView,在这个UIImageView上面有一个背景色清晰的UIView 我正在使用UIBezierPath在此UIView上徒手绘图,这样看来用户正在UIImageView的UIImage上绘图 我正在为UIBezierPath颜色添加透明度 我的问题是,当我最初在UIImageView中加载UIImage时,如何使UIImage看起来是灰度的,当用户在屏幕上移动手指时,UIImage的原始颜色显示给用户?正确的方法是使用核心图像,并编写自己的核心图像过

在我的应用程序中,我有一个
UIImageView
,在这个
UIImageView
上面有一个背景色清晰的
UIView

我正在使用
UIBezierPath
在此UIView上徒手绘图,这样看来用户正在
UIImageView
UIImage
上绘图

我正在为
UIBezierPath
颜色添加透明度


我的问题是,当我最初在
UIImageView
中加载
UIImage
时,如何使
UIImage
看起来是灰度的,当用户在屏幕上移动手指时,
UIImage
的原始颜色显示给用户?

正确的方法是使用核心图像,并编写自己的核心图像过滤器,对彩色图像进行操作。通过在过滤器上设置一些属性,它将知道以颜色绘制某些区域,但将其他区域转换为灰度


也就是说,这不是一个微不足道的工作项。你也许可以把其他东西拼凑在一起,但它可能会很不平稳。Core Image filter与Core Animation(靠近GPU)的工作级别相同,因此如果您这样做,它会工作得非常好。

触摸时,调用此函数可返回灰度图像。触摸结束后,为UIImageView设置原始图像

- (UIImage*)imageWithGrayScaleImage:(UIImage*)inputImg
{   
//Creating image rectangle
//CGRect imageRect = CGRectMake(0, 0, inputImg.size.width / (CGFloat)[inputImg scale], inputImg.size.height / (CGFloat)[inputImg scale]);
CGRect imageRect = CGRectMake(0, 0, inputImg.size.width, inputImg.size.height);

//Allocating memory for pixels
int width = imageRect.size.width;
int height = imageRect.size.height;

uint32_t *pixels = (uint32_t*)malloc(width * height * sizeof(uint32_t));

//Clearing the memory to preserve any transparency.(Alpha)
memset(pixels, 0, width * height * sizeof(uint32_t));

//Creating a context with RGBA pixels
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

//Drawing the bitmap to the context which will fill the pixels memory
CGContextDrawImage(context, imageRect, [inputImg CGImage]);

//Indices as ARGB or RGBA
const int RED = 1;
const int GREEN = 2;
const int BLUE = 3;

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        uint8_t* rgbaPixel = (uint8_t*)&pixels[y * width + x];

        //Calculating the grayScale value
        uint32_t grayPixel = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];

        //Setting the pixel to gray
        rgbaPixel[RED] = grayPixel;
        rgbaPixel[GREEN] = grayPixel;
        rgbaPixel[BLUE] = grayPixel;
    }
}

//Creating new CGImage from the context with modified pixels
CGImageRef newCGImage = CGBitmapContextCreateImage(context);

//Releasing resources to free up memory
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);

//Creating UIImage for return value
//UIImage* newUIImage = [UIImage imageWithCGImage:newCGImage scale:(CGFloat)[inputImg scale] orientation:UIImageOrientationUp];
UIImage* newUIImage = [UIImage imageWithCGImage:newCGImage];

//Releasing the CGImage
CGImageRelease(newCGImage);

return newUIImage;
}
-(UIImage*)图像带灰度图像:(UIImage*)输入
{   
//创建图像矩形
//CGRect imageRect=CGRectMake(0,0,inputImg.size.width/(CGFloat)[inputImg scale],inputImg.size.height/(CGFloat)[inputImg scale]);
CGRect imageRect=CGRectMake(0,0,inputImg.size.width,inputImg.size.height);
//为像素分配内存
int width=imageRect.size.width;
int height=imageRect.size.height;
uint32_t*像素=(uint32_t*)malloc(宽度*高度*尺寸(uint32_t));
//清除内存以保持任何透明度。(Alpha)
memset(像素,0,宽度*高度*大小(uint32_t));
//使用RGBA像素创建上下文
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef context=CGBitmapContextCreate(像素、宽度、高度、8、宽度*sizeof(uint32_t)、颜色空间、kCGBitmapByteOrder32Little | KCGimageAlphaPremultipledLast);
//将位图绘制到将填充像素内存的上下文中
CGContextDrawImage(上下文,imageRect,[inputImg CGImage]);
//索引为ARGB或RGBA
常数int RED=1;
常数int绿色=2;
常数int蓝色=3;
对于(int y=0;y
首先,我尝试使用Core image filter,但速度非常慢,因此我将此图像视图应用为uiview的内容视图,以便在uiview上绘制时,它似乎位于图像上。我的观点是,下部视图将具有过滤器,您将继续使用位于上方的视图来处理图形。您使用矢量指令编写了过滤器?确切地说-您正在drawrect中进行工作,而这不是使用核心图像过滤器所要做的。如果你想快速平滑,你必须花时间用向量语言做一个真正的过滤器。苹果有很多这样的例子,但你必须花时间来做这件事。谢谢,但我想删除触摸法中的灰色。在上面的方法中,它对视图中的所有点使用for循环,因此速度会非常慢。我试过上述方法。我想要的效果,在色彩飞溅的应用。