最近做了一个在UIView 上面画图片,然后给在图片上面见一层半透明Layer得 iphone Demo,上传上来供大家交流,讨论.如果有更好得实现希望各位不吝赐教。
在UIView drawRect 里面进行编码,详细代码如下:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage* img = [UIImage imageNamed: @"bg.png"];
CGContextSaveGState(context);
//Modify Coordinate system
CGContextTranslateCTM(context, 0, self.frame.size.height);
CGContextScaleCTM(context, 1, -1);
//draw bg image
CGContextDrawImage(context, self.frame, img.CGImage);
//draw layer
CGLayerRef aboveLayer = CGLayerCreateWithContext(context, self.frame.size, NULL);
CGContextRef aboveContext = CGLayerGetContext(aboveLayer);
CGFloat vc[] = {0.5, 0.5, 0.5, 0.5};
CGContextSetFillColor(aboveContext, vc);
CGContextFillRect(aboveContext, self.frame);
CGContextDrawLayerInRect(context, self.frame, aboveLayer);//draw layer
CGLayerRelease(aboveLayer);//release layer
CGContextRestoreGState(context);
}
程序原图(bg.png):
运行结果: