OGeek|极客世界-中国程序员成长平台

标题: IOS:仅对透明像素着色 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:11
标题: IOS:仅对透明像素着色

如果我有一个带有一些透明像素的图像,是否有可能用白色为透明像素着色并在objective-c 中使图像的其余部分透明?

谢谢!



Best Answer-推荐答案


我找到了解决办法:

- (UIImage*)convertToInverseWhiteMask: (UIImage *) image {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Draw a white background (for white mask)
    CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
    CGContextFillRect(ctx, imageRect);

    // Apply the source image's alpha
    [image drawInRect:imageRect blendMode:kCGBlendModeDestinationOut alpha:1.0f];

    UIImage* mask = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return mask; 
   }

关于IOS:仅对透明像素着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19139461/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4