ios - 图片来自颜色问题

标签 ios iphone uiimage uicolor

我正在使用下面的方法:

+ (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 .我的问题是返回的图像在参数中没有相同的颜色。

最佳答案

我通过将 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;
} 

关于ios - 图片来自颜色问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28216563/

相关文章:

ios - Xcode UI Test 的屏幕截图文件在哪里?

iphone - 自动 C++ 内存/对象实例管理?智能指针?

iphone - viewWillDisappear 中 NSString 的问题

从主队列调用 dispatch_sync 时 iOS 死锁,执行的 block 保存到核心数据

ios - 无法打开文档 "Main.Storyboard"。无法识别的文件内容

iphone - UIGraphicsBeginImageContextWithOptions 无提示崩溃

iphone - 为用户提供激活飞行模式的选项

ios - 如何使用 switch 语句来查找是否按下了 UIButton?

ios - 在 iOS swift 中存储大图片

swift - 所选图像未从 YPImagePicker 显示