2015-02-03 9 views

risposta

6

È inoltre possibile utilizzare questa estensione:

extension UIImage { 
    func imageWithColor(tintColor: UIColor) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) 

     let context = UIGraphicsGetCurrentContext() as CGContextRef 
     CGContextTranslateCTM(context, 0, self.size.height) 
     CGContextScaleCTM(context, 1.0, -1.0); 
     CGContextSetBlendMode(context, kCGBlendModeNormal) 

     let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect 
     CGContextClipToMask(context, rect, self.CGImage) 
     tintColor.setFill() 
     CGContextFillRect(context, rect) 

     let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage 
     UIGraphicsEndImageContext() 

     return newImage 
    } 
} 

E poi

image.imageWithColor("#1A6BAE".UIColor) 
3

Probabilmente è meglio usare la proprietà backgroundColor di UIImageView. È possibile farlo nel seguente modo -

self.imageView.backgroundColor = UIColor.redColor() 

i seguenti colori predefiniti esistono:

blackColor 
darkGrayColor 
lightGrayColor 
whiteColor 
grayColor 
redColor 
greenColor 
blueColor 
cyanColor 
yellowColor 
magentaColor 
orangeColor 
purpleColor 
brownColor 
clearColor 

Se invece volete creare a livello di codice un UIImage con un determinato colore, si può fare questo nel modo seguente:

var rect = CGRectMake(0, 0, size.width, size.height) 
UIGraphicsBeginImageContextWithOptions(size, false, 0) 
color.setFill() 
UIRectFill(rect) 
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

Dove l'immagine "var" è la tua UIImage colorata.

+0

questa proprietà backgroundColor funziona bene ma quando salvi l'immagine il colore di sfondo è ancora bianco –

+0

quindi uso questo codice UIGraphicsBeginImageContextWithOptions (CGSizeMake (120,120), false, 1.0) let rect2 = CGRectMake (0, 0, displayImage.size. . larghezza, displayImage.size.height) let context = UIGraphicsGetCurrentContext() UIColor.redColor() set() CGContextFillRect (contesto, rect2) displayImage.drawInRect (rect2) immagine = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // displayImage è una dimensione di 100 * 100 –

4

Aggiornato @ soluzione Egghead per Swift 3

extension UIImage { 
    static func imageWithColor(tintColor: UIColor) -> UIImage { 
     let rect = CGRect(x: 0, y: 0, width: 1, height: 1) 
     UIGraphicsBeginImageContextWithOptions(rect.size, false, 0) 
     tintColor.setFill() 
     UIRectFill(rect) 
     let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 

Usage:

UIImage.imageWithColor(tintColor: <Custom color>) 
3

Un altro modo per ottenere un'immagine con un colore di sfondo con un'estensione è: (Swift 3)

extension UIImage { 

    /** 
     Returns an UIImage with a specified background color. 
     - parameter color: The color of the background 
    */ 
    convenience init(withBackground color: UIColor) { 

     let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1) 
     UIGraphicsBeginImageContext(rect.size); 
     let context:CGContext = UIGraphicsGetCurrentContext()!; 
     context.setFillColor(color.cgColor); 
     context.fill(rect) 

     let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 

     self.init(ciImage: CIImage(image: image)!) 

    } 
} 

E poi:

// change UIColor.white with your color 
let image = UIImage(withBackground: UIColor.white) 
Problemi correlati