亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在iOS上以程序方式著色圖像?

如何在iOS上以程序方式著色圖像?

iOS
大話西游666 2019-11-05 15:29:48
我想為圖像添加色彩參考。結果應該看起來像Photoshop中的“乘法”混合模式,其中白色將替換為“ 色調”:我將不斷更改顏色值。后續:我會將代碼執行此操作,并將其放入ImageView的drawRect:方法中,對嗎?像往常一樣,與鏈接相反,代碼片段將極大地幫助我理解。更新:用建議的代碼Ramin子類化UIImageView 。我把它放在我的視圖控制器的viewDidLoad:中:[self.lena setImage:[UIImage imageNamed:kImageName]];[self.lena setOverlayColor:[UIColor blueColor]];[super viewDidLoad];我看到了圖像,但是沒有著色。我還嘗試加載其他圖像,在IB中設置圖像,然后在視圖控制器中調用setNeedsDisplay:。更新:未調用drawRect :。最終更新:我發現一個舊項目已正確設置了imageView,因此我可以測試Ramin的代碼,并且它的工作原理很吸引人!最終,最終更新:對于那些剛剛了解Core Graphics的人來說,這是可能可行的最簡單的方法。在子類化的UIView中:- (void)drawRect:(CGRect)rect {    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1].CGColor)); // don't make color too saturated    CGContextFillRect(context, rect); // draw base    [[UIImage imageNamed:@"someImage.png"] drawInRect: rect blendMode:kCGBlendModeOverlay alpha:1.0]; // draw image}
查看完整描述

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值來調整混合級別)。


查看完整回答
反對 回復 2019-11-05
?
寶慕林4294392

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


查看完整回答
反對 回復 2019-11-05
?
夢里花落0921

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


查看完整回答
反對 回復 2019-11-05
  • 3 回答
  • 0 關注
  • 542 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號