Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-仅在非透明区域使用颜色覆盖UIImage? 我有一个白色的图像,中间有一个透明区域:_Ios_Objective C_Uiimage - Fatal编程技术网

iOS-仅在非透明区域使用颜色覆盖UIImage? 我有一个白色的图像,中间有一个透明区域:

iOS-仅在非透明区域使用颜色覆盖UIImage? 我有一个白色的图像,中间有一个透明区域:,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我想用颜色(仅非透明区域)覆盖它,使其看起来像这样: 我该怎么做 我已经研究过了,但它覆盖了整个图像,包括不透明区域。因此,您的图像实际上是一个“遮罩”。使用“CGContextClipToMask”调用和您引用的上述代码进行操作。尝试以下代码,它会给出您想要的确切信息 UIGraphicsBeginImageContextWithOptions(imgView.image.size, YES, 0); CGContextRef context = UIGraphicsGetC

我想用颜色(仅非透明区域)覆盖它,使其看起来像这样:

我该怎么做


我已经研究过了,但它覆盖了整个图像,包括不透明区域。

因此,您的图像实际上是一个“遮罩”。使用“CGContextClipToMask”调用和您引用的上述代码进行操作。

尝试以下代码,它会给出您想要的确切信息

    UIGraphicsBeginImageContextWithOptions(imgView.image.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillRect(context, (CGRect){ {0,0}, imgView.image.size} );

    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, imgView.image.size.height);
    CGContextConcatCTM(context, flipVertical);
    CGContextDrawImage(context, (CGRect){ {0,0}, imgView.image.size }, [imgView.image CGImage]);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [imgView setImage:image];