ios: change the colors of a UIImage

前端 未结 8 835
旧时难觅i
旧时难觅i 2020-11-27 12:50

I am working on an Iphone application.

I have png pictures that represents symbols. symbols are all black with a transparent background.

Is there a way I can

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 13:08

    hi use this category file to change the image entire color....

    .h file:

    #import 
    
    @interface UIImage (AddtionalFunctionalities)
    
    //TintColor...
    - (UIImage *)imageWithTint:(UIColor *)tintColor;
    //scale and resize...
    -(UIImage*)scaleToSize:(CGSize)size;
    
    @end
    

    .m file:

    #import "UIImage+AddtionalFunctionalities.h"
    
    @implementation UIImage (AddtionalFunctionalities)
    
    
    - (UIImage *)imageWithTint:(UIColor *)tintColor 
    {
        // Begin drawing
        CGRect aRect = CGRectMake(0.f, 0.f, self.size.width, self.size.height);
        CGImageRef alphaMask;
    
        //
        // Compute mask flipping image
        //
        {
            UIGraphicsBeginImageContext(aRect.size);        
            CGContextRef c = UIGraphicsGetCurrentContext(); 
    
            // draw image
            CGContextTranslateCTM(c, 0, aRect.size.height);
            CGContextScaleCTM(c, 1.0, -1.0);
            [self drawInRect: aRect];
    
            alphaMask = CGBitmapContextCreateImage(c);
    
            UIGraphicsEndImageContext();
        }
    
        //
        UIGraphicsBeginImageContext(aRect.size);
    
        // Get the graphic context
        CGContextRef c = UIGraphicsGetCurrentContext(); 
    
        // Draw the image
        [self drawInRect:aRect];
    
        // Mask
        CGContextClipToMask(c, aRect, alphaMask);
    
        // Set the fill color space
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextSetFillColorSpace(c, colorSpace);
    
        // Set the fill color
        CGContextSetFillColorWithColor(c, tintColor.CGColor);
    
        UIRectFillUsingBlendMode(aRect, kCGBlendModeNormal);
    
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();    
    
        // Release memory
        CGColorSpaceRelease(colorSpace);
        CGImageRelease(alphaMask);
    
        return img;
    }
    
    
    -(UIImage*)scaleToSize:(CGSize)size
    {
        // Create a bitmap graphics context
        // This will also set it as the current context
        UIGraphicsBeginImageContext(size);
    
        // Draw the scaled image in the current context
        [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
        // Create a new image from current context
        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
        // Pop the current context from the stack
        UIGraphicsEndImageContext();
    
        // Return our new scaled image
        return scaledImage;
    }
    
    @end
    

    the method call will be :

     self.outputImage.image=[sourceImage imageWithTint:[UIColor redColor]];
    

    if u want to use the image means use this:

    self.outputImage.image=[sourceImage imageWithTint:[UIColor colorWithPatternImage:[UIImage imageNamed: @"red.jpg"]]];
    

    i hope this will help you...

提交回复
热议问题