【问题标题】:Overlay non transparent Pixels in iOS Cocoa在 iOS Cocoa 中覆盖非透明像素
【发布时间】:2011-04-05 23:34:35
【问题描述】:

iOS SDK 有没有办法用彩色像素覆盖图像中的不透明像素?


非常感谢两位回答。

我实现的最终解决方案使用子类 UIView 的 drawRect 方法中接受的答案中提到的代码,我使用以下代码覆盖颜色:

CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1].CGColor));

CGContextFillRect(context, area);

【问题讨论】:

    标签: objective-c ios


    【解决方案1】:

    我认为您可能正在寻找混合模式 kCGBlendModeSourceAtop。首先,将 UIImage 绘制到您的视图中。然后用

    获取当前图形上下文
    CGContext currentCtx = UIGraphicsGetCurrentContext();
    

    然后保存上下文状态,并设置混合模式:

    CGContextSaveGState(currentCtx);
    CGContextSetBlendMode(currentCtx, kCGBlendModeSourceAtop);
    

    然后你可以绘制任何你想要覆盖在 UIImage 的不透明像素上的东西。之后,只需重置上下文状态:

    CGContextRestoreGState(currentCtx);
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您可以使用混合模式修改某些内容的绘制方式。有关 iOS 上 Core Graphics 支持的混合模式的完整列表,请参阅 http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html%23//apple_ref/doc/c_ref/CGBlendMode。根据您的描述,我想您可能对 kCGBlendModeSourceIn 感兴趣,它使用旧内容的 alpha 值作为掩码绘制新内容,或者 kCGBlendModeSourceAtop,它使用旧内容的 alpha 作为掩码绘制新内容使用新内容的 alpha 值作为掩码在旧内容的顶部。您可以使用CGContextSetBlendMode为所有绘图设置混合模式,也可以使用-[UIImage drawAtPoint:blendMode:alpha:]绘制具有特定混合模式的UIImage。

      【讨论】:

        猜你喜欢
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        • 2017-01-18
        • 1970-01-01
        • 2016-07-09
        • 2016-08-02
        • 2017-05-21
        • 2015-08-12
        相关资源
        最近更新 更多