2009-07-29 68 views
2

我使用下面的代码来填充颜色iPhone的图像在图像的一部分。如何填充颜色只在在iPhone

- (UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor { 
UIGraphicsBeginImageContext(baseImage.size); 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height); 

CGContextScaleCTM(ctx, 1, -1); 
CGContextTranslateCTM(ctx, 0, -area.size.height); 

CGContextSaveGState(ctx); 
CGContextClipToMask(ctx, area, baseImage.CGImage); 

[theColor set]; 
CGContextFillRect(ctx, area); 

CGContextRestoreGState(ctx); 

CGContextSetBlendMode(ctx, kCGBlendModeMultiply); 

CGContextDrawImage(ctx, area, baseImage.CGImage); 

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

return newImage; 

}

但上面的代码填充整个图像中的颜色。我只想感受上半部分,比如图像的1/4。那么该怎么做呢?

+0

我想通了.. 变化CGContextFillRect到的线。 CGContextFillRect(ctx,CGRectMake(0,0,baseImage.size.width,100)); – rkb 2009-07-29 18:17:52

回答

1

我想通了..改变CGContextFillRect的线。 CGContextFillRect(ctx,CGRectMake(0,0,baseImage.size.width,100));

+0

好回答这个问题,HTTP://stackoverflow.com/questions/8124308/filling-a-portion-of-an-image-with-color – Marios 2011-11-18 10:32:43