Question

I am creating a simple drawing application in which I have a UIView in background & UIImageView in foreground. I am doing some drawing stuff in UIView and I have set an image in UIImageView. I want to add the transparency effect in UIImageView to show the lines behind the image. I know I can do this by reducing alpha, but I don’t want to change the alpha of image.

I want to do it with CGContextSetBlendMode, but I don’t know how to do this. Kindly help me to resolve this issue.

Thanks!

IMAGE http://www.freeimagehosting.net/q3237

UIImage *img = [UIImage imageNamed:@"Image.png"];      

UIGraphicsBeginImageContext(self.view.frame.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSetBlendMode(ctx, kCGBlendModeMultiply); 
[img drawInRect:CGRectMake(0, 0, 768, 1004) blendMode:kCGBlendModeDarken alpha:1]; [imageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)]; 
CGContextSetBlendMode(ctx, kCGBlendModeDarken); 
imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
Was it helpful?

Solution

No need to keep imageview do it like this.. Since you are drawing on two different context you wont be able to use blend modes across them. For that you need to draw other stuff on your drawing view and then draw your image..

  - (void)drawRect:(CGRect)rect {

         // do ur drawing stuff first             

            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextTranslateCTM(context, 0, self.bounds.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextDrawImage(context, self.bounds, self.image.CGImage);
            CGContextSetBlendMode(context, kCGBlendModeSaturation);
            CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
            CGContextFillRect(context, rect);  
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top