2009-07-29 105 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