iOS在UIImage上的绘制
我需要画一个图像涂鸦风格,如下所示。我的问题是,我需要有能力在不删除UIImage部分的情况下删除我绘制的线条。目前,我正在考虑使用一个图像作为背景图像,另一个具有透明背景的图像作为涂鸦绘图,然后在绘图完成后将两者结合起来。有更好的办法吗iOS在UIImage上的绘制,ios,uiimage,drawing,Ios,Uiimage,Drawing,我需要画一个图像涂鸦风格,如下所示。我的问题是,我需要有能力在不删除UIImage部分的情况下删除我绘制的线条。目前,我正在考虑使用一个图像作为背景图像,另一个具有透明背景的图像作为涂鸦绘图,然后在绘图完成后将两者结合起来。有更好的办法吗 - (void)drawRect:(CGRect)rect { //Get drawing context CGContextRef context = UIGraphicsGetCurrentContext(); //Crea
- (void)drawRect:(CGRect)rect
{
//Get drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Create drawing layer if required
if(drawingLayer == nil)
{
drawingLayer = CGLayerCreateWithContext(context, bounds.size, NULL);
CGContextRef layerContext = CGLayerGetContext(drawingLayer);
CGContextScaleCTM(layerContext, scale, scale);
self.viewRect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.width);
NSLog(@"%f %f",self.viewRect.size.width,self.viewRect.size.width);
}
//Draw paths from array
int arrayCount = [pathArray count];
for(int i = 0; i < arrayCount; i++)
{
path = [pathArray objectAtIndex:i];
UIBezierPath *bezierPath = path.bezierPath;
CGContextRef layerContext = CGLayerGetContext(drawingLayer);
CGContextAddPath(layerContext, bezierPath.CGPath);
CGContextSetLineWidth(layerContext, path.width);
CGContextSetStrokeColorWithColor(layerContext, path.color.CGColor);
CGContextSetLineCap(layerContext, kCGLineCapRound);
if(activeColor == 4)
{
CGContextSetBlendMode(layerContext, kCGBlendModeClear);
}
else
{
CGContextSetBlendMode(layerContext, kCGBlendModeNormal);
}
CGContextStrokePath(layerContext);
}
if (loadedImage == NO)
{
loadedImage = YES;
CGContextRef layerContext = CGLayerGetContext(drawingLayer);
CGContextSaveGState(layerContext);
UIGraphicsBeginImageContext (self.viewRect.size);
CGContextTranslateCTM(layerContext, 0, self.viewRect.size.height);
CGContextScaleCTM(layerContext, 1.0, -1.0);
CGContextDrawImage(layerContext, self.viewRect, self.image.CGImage);
UIGraphicsEndImageContext();
CGContextRestoreGState(layerContext);
}
[pathArray removeAllObjects];
CGContextDrawLayerInRect(context, self.viewRect, drawingLayer);
}
-(void)drawRect:(CGRect)rect
{
//获取图形上下文
CGContextRef context=UIGraphicsGetCurrentContext();
//如果需要,创建图形图层
if(drawingLayer==nil)
{
drawingLayer=CGLayerCreateWithContext(context,bounds.size,NULL);
CGContextRef layerContext=CGLayerGetContext(drawingLayer);
CGContextScaleTM(层上下文、比例、比例);
self.viewRect=CGRectMake(0,0,self.bounds.size.width,self.bounds.size.width);
NSLog(@“%f%f”,self.viewRect.size.width,self.viewRect.size.width);
}
//从数组中绘制路径
int arrayCount=[PathArrayCount];
for(int i=0;i
在UIImageView中设置图像,并在图像视图顶部使用普通UIView进行绘制/擦除等。保存时,只需使用绘制内容更新图像,在图像视图中重置图像并在UIView上清除绘制。在UIImageView中设置图像,并在图像视图顶部使用普通UIView进行绘制/擦除等。保存时,只需使用绘制的内容更新图像,在图像视图中重置图像,并在UIView上清除绘制。