2014-11-17 8 views
9

Devo mostrare un'immagine colorata a tinta in CALayer.Come mostrare l'immagine colorata a tinta unita in CALayer su iOS?

funziona per UIImageView:

imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 

Ma CALayer mostra l'immagine con il suo colore originale e non il colore della tinta.

let tintedImage = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 
layer.contents = tintedImage.CGImage 

C'è un modo per mostrare anche l'immagine colorata in CALayer?

Demo:

https://github.com/exchangegroup/calayer-with-tint-colored-image

enter image description here

risposta

18

Credo che la mia soluzione è molto meno ingombrante:

 let maskLayer = CALayer() 
     maskLayer.frame = layer.bounds 
     maskLayer.contents = tintedImage.CGImage 
     layer.mask = maskLayer 
     layer.backgroundColor = UIColor.redColor().CGColor 

fatemi sapere se funziona bene :)

+1

molto più semplice e più pulito della risposta accettata. Potresti voler renderlo più completo aggiungendo let layer = CALayer() e impostando layer.frame. –

+0

Non funziona per me. Quindi scrivi il mio codice: – Markus

3

I Immagino che sia possibile su OSX usando il filtro proprietà di CALAYER, ma in ios non viene utilizzato. Penso che si dovrebbe ridisegnare l'immagine del tutto, ecco un esempio di codice, si tinge tutto ciò che ha alpha> 0.

- (UIImage *)tintedImageWithColor:(UIColor *)tintColor blendingMode:(CGBlendMode)blendMode highQuality:(BOOL) yerOrNo; 
{ 
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f); 
    if (yerOrNo) { 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGContextSetShouldAntialias(context, true); 
     CGContextSetAllowsAntialiasing(context, true); 
     CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    } 
    [tintColor setFill]; 
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height); 
    UIRectFill(bounds); 
    [self drawInRect:bounds blendMode:blendMode alpha:1.0f]; 

    if (blendMode != kCGBlendModeDestinationIn) 
     [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0]; 

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

    return tintedImage; 
} 

ho trovato questo frammento su internet, ma non ricordo dove.

-1

Riferendosi al commento sopra.

Il mio codice:

button.backgroundColor = UIColor.withAlphaComponent(UIColor.white)(0.1) 

    button.layer.cornerRadius = button.frame.size.height/2 

    button.layer.borderWidth = 3 

    button.layer.borderColor = UIColor.white.cgColor 

    button.layer.contents = UIImage(named: "buttonImage.png")?.cgImage 

    button.layer.contentsGravity = kCAGravityCenter 

    button.layer.magnificationFilter = kCAFilterLinear 

    button.layer.isGeometryFlipped = false 

Applicando la soluzione:

let maskLayer = CALayer() 

E infine:

layer.backgroundColor = UIColor.redColor().CGColor 

Il risultato è orribile.

La soluzione è sul seguente link (link nel commento)

Ma mi mostrano il mio codice (prendere da link):

  let image_ = UIImage(named: "image.png") 

      let maskImage = image_?.cgImage 

      let width = image_?.size.width 
      let height = image_?.size.height 

      let bounds = CGRect(x: 0, y: 0, width: width!, height: height!) 

      let colorSpace = CGColorSpaceCreateDeviceRGB() 

      let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) 

      let bitmapContext = CGContext(data: nil, 
              width: Int(width!), 
              height: Int(height!), 
              bitsPerComponent: 8, 
              bytesPerRow: 0, 
              space: colorSpace, 
              bitmapInfo: bitmapInfo.rawValue) 

      bitmapContext?.clip(to: bounds, mask: maskImage!) 

      bitmapContext?.setFillColor(UIColor.yellow.cgColor) 

      bitmapContext?.fill(bounds) 

      let cImage = bitmapContext?.makeImage() 

      let coloredImage = UIImage(cgImage: cImage!) 


      self.myUIButton.layer.contents = coloredImage.cgImage 

Ti prego, lascia che ti dica una cosa (è importante) non giocare con:

Il nome dell'immagine "someImage.png" deve essere esattamente il nome del file nella cartella del progetto.

+0

Ho trovato la soluzione e funziona per me (quasi per me) Ho allegato il link: https: // StackOverflow.com/domande/31803157/how-to-color-a-UIImage-in-swift/31803460 # 31803460 – Markus

Problemi correlati