0

我正在尝试一种有效的方法来删除核心图形图像过滤器,这里是过滤器的代码,它将棕褐色效果应用于 UIImage

-(UIImage*)applyFilter {

    CGImageRef inImage = self.CGImage;
    CFDataRef m_dataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
    UInt8* m_pixelBuf = (UInt8*)CFDataGetBytePtr(m_dataRef);

    int length = CFDataGetLength(m_dataRef);

    for (int i=0; i<length; i+=4) {
        filterSepia(m_pixelBuf, i);
    }

    CGContextRef ctx = CGBitmapContextCreate(m_pixelBuf,
                                         CGImageGetWidth(inImage),
                                         CGImageGetHeight(inImage),
                                         CGImageGetBitsPerComponent(inImage),
                                         CGImageGetBytesPerRow(inImage),
                                         CGImageGetColorSpace(inImage),
                                         CGImageGetBitmapInfo(inImage));

    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
    CGContextRelease(ctx);
    UIImage* finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CFRelease(m_dataRef);
    return finalImage;
}

void filterSepia(UInt8 *pixelBuf, UInt32 offset) {

    int r = offset;
int g = offset+1;
int b = offset+2;

int red = pixelBuf[r];
int green = pixelBuf[g];
int blue = pixelBuf[b];

    pixelBuf[r] = SAFECOLOR((red*0.393)+(green*0.769)+(blue*0.189));
    pixelBuf[g] = SAFECOLOR((red*0.349)+(green*0.686)+(blue*0.168));
    pixelBuf[b] = SAFECOLOR((red*0.272)+(green*0.534)+(blue*131));
}

现在我想有效地删除这个效果或类似的东西我可以在按下切换按钮时做这样的事情

在标题中:

#define SAFECOLOR(color) MIN(255, MAX(0,color))
BOOL toggleOn;
@property (nonatomic, retain)UIImage* image;

在实施中:

-(IBAction)sepiaButton:(id)sender {

    UIImage* original = image;
    if (toggleOn) {
        [image applyFilter];
    } else {
        image = original;
    }
    toggleOn =! toggleOn
}

但是,由于应用了多个图像过滤器,这会变得效率低下,还是有更好的方法来做到这一点?

4

1 回答 1

1

为什么要继续生成相同的图像而不是预渲染它们?保留原始版本的副本并简单地隐藏覆盖的视图。

于 2012-11-10T21:03:40.680 回答