How to change the color of an UIImage

后端 未结 7 495

I want not to change the backgroundColor of an UIImage,
but rather to change the color of the whole image.

Because I have got standard forms which I can move fr

7条回答
  •  不知归路
    2020-12-02 08:15

    UIImage *image = [UIImage imageNamed:@"triangle.png"];
    
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToMask(context, rect, image.CGImage);
    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImage *flippedImage = [UIImage imageWithCGImage:img.CGImage 
                                                scale:1.0 orientation: UIImageOrientationDownMirrored];
    
    yourUIImageView.image = flippedImage;
    

提交回复
热议问题