2015-02-03 13 views

Odpowiedz

6

Można również użyć tego rozszerzenia:

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 
    } 
} 

A potem

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

Najlepiej używać właściwości backgroundColor swojego UIImageView. Można to zrobić w następujący sposób -

self.imageView.backgroundColor = UIColor.redColor() 

Następujące zadane kolory istnieją:

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

Jeśli zamiast tego chcesz programowo utworzyć UIImage z danego koloru, można to zrobić w następujący sposób:

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

Gdzie "obraz" var jest Twoim kolorowym UIImage.

+0

to właściwość backgroundColor działa poprawnie, ale kiedy zapisuję obraz, kolor tła jest nadal biały –

+0

więc używam tego kodu UIGraphicsBeginImageContextWithOptions (CGSizeMake (120,120), false, 1.0) let rect2 = CGRectMake (0, 0, displayImage.size. . szerokość displayImage.size.height) Let context = UIGraphicsGetCurrentContext() UIColor.redColor() zestawu() CGContextFillRect (kontekst rect2) displayImage.drawInRect (rect2) grafika = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // displayImage ma rozmiar 100 * 100 –

4

Aktualizacja @ rozwiązania egghead dla 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 
    } 
} 

Zastosowanie:

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

Innym sposobem na uzyskanie obrazu z kolorem tła z rozszerzeniem jest: (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)!) 

    } 
} 

A potem:

// change UIColor.white with your color 
let image = UIImage(withBackground: UIColor.white) 
Powiązane problemy