使用Core Graphics

时间:2015-06-18 13:26:22

标签: ios objective-c uiimage core-graphics alpha

我需要使用UIImage并添加半透明层以生成新的UIImage。我想我已经接近但有些事情仍然是错误的。这是我的代码:

- (UIImage*) addLayerTo:(UIImage*)source
{
    CGSize size = [source size];
    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:0.18];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0.2, 0.5, 0.1, 0.18);
    CGContextFillRect(context, rect);
    UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return testImg;
}

2 个答案:

答案 0 :(得分:3)

您忘记在当前上下文中绘制要与source图像混合的当前图像。

- (UIImage*) addLayerTo:(UIImage*)source
{
  CGSize size = [source size];
  UIGraphicsBeginImageContext(size, NO, [UIScreen mainScreen].scale); // Use this image context initialiser instead

  CGRect rect = CGRectMake(0, 0, size.width, size.height);
  [self drawInRect: rect] // Draw the current image in context
  [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:0.18]; // Blend with other image

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetRGBStrokeColor(context, 0.2, 0.5, 0.1, 0.18);
  CGContextFillRect(context, rect);
  UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return testImg;

}

答案 1 :(得分:0)

<ItemsControl ItemsSource="{Binding Path=IterateProperty}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding DataContext.GetThis,
                                RelativeSource={RelativeSource Mode=FindAncestor,
                                                               AncestorType={x:Type ItemsControl}}}" />