3 回答

TA貢獻1798條經驗 獲得超3個贊
首先,您需要繼承UIImageView并重寫drawRect方法。您的類需要一個UIColor屬性(我們將其稱為overlayColor)來保存混合顏色,以及一個自定義設置器,當顏色更改時,該設置器將強制重繪。像這樣:
- (void) setOverlayColor:(UIColor *)newColor {
if (overlayColor)
[overlayColor release];
overlayColor = [newColor retain];
[self setNeedsDisplay]; // fires off drawRect each time color changes
}
在drawRect方法中,您將要先繪制圖像,然后在其上覆蓋一個填充有所需顏色的矩形以及適當的混合模式,如下所示:
- (void) drawRect:(CGRect)area
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// Draw picture first
//
CGContextDrawImage(context, self.frame, self.image.CGImage);
// Blend mode could be any of CGBlendMode values. Now draw filled rectangle
// over top of image.
//
CGContextSetBlendMode (context, kCGBlendModeMultiply);
CGContextSetFillColor(context, CGColorGetComponents(self.overlayColor.CGColor));
CGContextFillRect (context, self.bounds);
CGContextRestoreGState(context);
}
通常,為了優化圖形,您可以將實際圖形限制為僅傳遞給drawRect的區域,但是由于每次更改顏色后都必須重新繪制背景圖像,因此很有可能需要刷新整個圖形。
要使用它,請創建對象的實例,然后將image屬性(從UIImageView繼承)設置為圖片和overlayColorUIColor值(可以通過更改向下傳遞的顏色的alpha值來調整混合級別)。

TA貢獻2021條經驗 獲得超8個贊
在iOS7中,他們在UIImageView上引入了tintColor屬性,在UIImage上引入了renderingMode。要在iOS7上著色UIImage,您要做的就是:
UIImageView* imageView = …
UIImage* originalImage = …
UIImage* imageForRendering = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.image = imageForRendering;
imageView.tintColor = [UIColor redColor]; // or any color you want to tint it with

TA貢獻1772條經驗 獲得超6個贊
我想用alpha著色圖像,并創建了以下課程。如果您發現任何問題,請告訴我。
我已經命名了我的類,CSTintedImageView并且它繼承UIView自類,因為UIImageView它沒有調用該drawRect:方法,就像前面的答復中提到的那樣。我已經設置了一個指定的初始化程序,該初始化程序類似于在UIImageView該類中找到的初始化程序。
用法:
CSTintedImageView * imageView = [[CSTintedImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
imageView.tintColor = [UIColor redColor];
CSTintedImageView.h
@interface CSTintedImageView : UIView
@property (strong, nonatomic) UIImage * image;
@property (strong, nonatomic) UIColor * tintColor;
- (id)initWithImage:(UIImage *)image;
@end
CSTintedImageView.m
#import "CSTintedImageView.h"
@implementation CSTintedImageView
@synthesize image=_image;
@synthesize tintColor=_tintColor;
- (id)initWithImage:(UIImage *)image
{
self = [super initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
if(self)
{
self.image = image;
//set the view to opaque
self.opaque = NO;
}
return self;
}
- (void)setTintColor:(UIColor *)color
{
_tintColor = color;
//update every time the tint color is set
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
//resolve CG/iOS coordinate mismatch
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);
//set the clipping area to the image
CGContextClipToMask(context, rect, _image.CGImage);
//set the fill color
CGContextSetFillColor(context, CGColorGetComponents(_tintColor.CGColor));
CGContextFillRect(context, rect);
//blend mode overlay
CGContextSetBlendMode(context, kCGBlendModeOverlay);
//draw the image
CGContextDrawImage(context, rect, _image.CGImage);
}
@end
- 3 回答
- 0 關注
- 542 瀏覽
添加回答
舉報