【问题标题】:Image from color issue图片来自颜色问题
【发布时间】:2015-03-28 18:54:39
【问题描述】:

我正在使用以下方法:

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

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

    return image;
}

来自link。我的问题是返回的图像在参数中没有相同的颜色。

【问题讨论】:

  • 您能否再解释一下“返回的图像在参数中没有相同的颜色”?
  • 例如输入颜色为 [UIColor colorWithRed:219./255。绿色:94./255。蓝色:8./255。 alpha:1.] 返回的图像代码为 R:208 G:73 B:12
  • 你如何检查它不是同一种颜色?
  • 我用 mac os 中的色度计应用程序检查了这一点,没有差异是显而易见的。

标签: ios iphone uiimage uicolor


【解决方案1】:

我通过将CGContextSetFillColorWithColor(context, [color CGColor]); 替换为[color setFill]; 解决了我的问题,新方法将类似于以下代码:

+ (UIImage *)imageFromColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
} 

【讨论】:

    猜你喜欢
    • 2014-08-14
    • 2018-03-23
    • 1970-01-01
    • 2015-02-15
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2016-10-26
    相关资源
    最近更新 更多