簡體   English   中英

CGContextSetFillColorWithColor無法與傳入的顏色變量一起使用

[英]CGContextSetFillColorWithColor not working with passed in color variable

我正在嘗試使用CGContextSetFillColorWithColor和從Parse數據庫中獲取的顏色變量為圖像着色。

通過這種方式更改圖像的顏色效果很好:

[...]
UIImage *image = [UIImage imageNamed:@"menuButton.png"];
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
[...]

那將menuButton.png更改為白色沒問題。

但是,當我嘗試將其更改為十六進制顏色時,我是通過以下方式從Parse(使用十六進制顏色實用程序)獲取的:

self.bgColor = [post objectForKey:@"bgColor"]; //bgColor = "#ffffff"
[...]
CGContextSetFillColorWithColor(context,
   [[UIColor colorWithHexString:self.bgColor] CGColor]);

圖像不再出現。 這是指針問題嗎? 謝謝你的幫助。

我遇到了類似的問題,沒有找到直接的解決方案。 對我來說,解決方案是在UIColor對象上使用-setFill方法,而不是使用中間CGColor對象:

[[UIColor colorWithHexString:self.bgColor] setFill];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM