0

I have a UIView subclass that I would like to use to draw images with different blend modes.

code:

@implementation CompositeImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)setBlendMode:(CGBlendMode) composite
{
    blender = composite;
}

-(void)setImage:(UIImage*) img
{
    image = img;
}

- (void)drawRect:(CGRect)rect
{
    NSLog(@"it draws");
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(c, blender);
    CGContextDrawImage(c, rect, image.CGImage);
}

@end

the code that I use to set it up it is:

[testImage setImage:[UIImage imageNamed:@"Prayer_Background_Paid"]];
[testImage setBlendMode:kCGBlendModeColor];
[testImage setNeedsDisplay];

I am using the interface builder to place a large rectangular UIView, and then set its class to CompositeImageView. However, it still draws as a large white square. Even if I comment out everything inside drawRect, it still draws the white square. However, it IS calling drawRect, because "it draws" is being logged.

Hailei
  • 42,163
  • 6
  • 44
  • 69
Nyth
  • 582
  • 5
  • 16
  • Are you sure that your image is not `nil` (perhaps a typo in the file name)? – omz Apr 28 '12 at 03:30
  • yes. I also tried a number of other images in the project. Unless there is something wrong with the way I'm getting/drawing the image, then I have no idea what could be wrong--hence why I posted here. (not sure whats with the negative votes) – Nyth May 07 '12 at 16:20
  • Were you able to make this work? I'm trying to do the same thing right now. – Zoltán Matók Jul 27 '14 at 11:31
  • Not in the way I had originally intended. The views draw themselves in drawRect and THEN are composited on the GPU. As a result, it can't blend with the contents below the image. You might be able to achieve something similar with [UIVisualEffectsView](https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIVisualEffectView/index.html) but I'm not familiar with the internals of that guy either. If you're just trying to draw an image on top of your background color, or another image, you can just draw that content before the call to CGContextSetBlendMode(). – Nyth Jul 27 '14 at 16:59
  • If you absolutely needed the effect, you could snapshot the views below and draw that image with the correct offset prior to drawing the blended image, but that starts getting very complicated very quickly if you want to make it run in real-time. You could fake it pretty well if the "background" content doesn't change too often, though. – Nyth Jul 27 '14 at 17:05

1 Answers1

0

Are you sure kCGBlendModeColor is what you want? From the Apple doc:

Uses the luminance values of the background with the hue and saturation values of the source image.

It seems that if it was a white background, the blended image would also appear white.

Jesse Gumpo
  • 4,777
  • 1
  • 20
  • 29
  • Color was just one of them that I tried--As I mentioned, I attempted to completely comment out the drawRect method entirely and the white rectangle still occurred. – Nyth Apr 28 '12 at 02:12
  • try setting self.backgroundColor = [UIColor clearColor]; in init. – Jesse Gumpo Apr 28 '12 at 02:19
  • setting the background color to clear made it not draw at all, although it's still calling the drawRect method... – Nyth Apr 28 '12 at 04:57
  • Actually, when I call [testImage setbackgroundColor:[UIColor clearColor]] it doesn't draw anything. When I place [self setBackgroundColor...] in the drawRect method, it still draws the white square. – Nyth Apr 28 '12 at 05:03