【问题标题】:objective-c - Create an UIImage with Core Graphicsobjective-c - 使用 Core Graphics 创建 UIImage
【发布时间】:2012-03-22 19:27:13
【问题描述】:

我有这段代码,但它没有向视图添加任何内容,你能帮我找出问题所在吗? 我也会对图像应用一个过滤器,但这将在稍后完成,但如果我要添加过滤器,请告诉我这是否是错误的方法。

UIGraphicsPushContext(context); 

CALayer *layer = [CALayer layer];
CGColorRef bgColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor;
layer.frame = CGRectMake(0, 0, 200, 200);

CGContextSetFillColorWithColor(context, bgColor);
CGContextFillRect(context, layer.bounds);

CGContextSaveGState(context);
CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
CGContextSetFillColorSpace(context, patternSpace);
CGColorSpaceRelease(patternSpace);

CGContextFillRect(context, layer.bounds);
CGContextRestoreGState(context);    
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *borrar = [[UIImageView alloc]initWithImage:image];

[self.view addSubview:borrar];

【问题讨论】:

    标签: objective-c core-graphics


    【解决方案1】:

    您永远不会打开图像上下文。使用UIGraphicsBeginImageContextWithOptions

    【讨论】: