2011-01-13 10 views

risposta

49

Si potrebbe raggiungere questo obiettivo con una semplice categoria su UIImage:

@interface UIImage (Tint) 

- (UIImage *)tintedImageUsingColor:(UIColor *)tintColor; 

@end 

@implementation UIImage (Tint) 

- (UIImage *)tintedImageUsingColor:(UIColor *)tintColor { 
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); 
    CGRect drawRect = CGRectMake(0, 0, self.size.width, self.size.height); 
    [self drawInRect:drawRect]; 
    [tintColor set]; 
    UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop); 
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return tintedImage; 
} 

@end 

Per l'effetto mostrato sopra, ci si passa qualcosa come [UIColor colorWithWhite:0.0 alpha:0.3] come parametro tintColor (esperimento con il valore alfa).

+0

Grandi cose !!!! – hfossli

+1

A differenza di altre soluzioni che galleggiano intorno a SO, questo ha il piacevole effetto di non colorare l'intero rect. Buon uso di UIRectFillUsingBlendMode() –

+0

Ottima risposta, grazie. – titaniumdecoy