29,028
社区成员
发帖
与我相关
我的任务
分享
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
把context保存到image里
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
把context保存到image里[/quote]
1. 你这个函数是在drawRect里面调用的嘛?
因为当你在
drawRect里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,UIGraphicsBeginImageContext返回的上下文是空的,
但是你若是在touchedMoved里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,在UIGraphicsBeginImageContext代码被调用之前,此时的当前上下文是什么?[/quote]
CGContextRef context = UIGraphicsGetCurrentContext();这个绘制的上下文是必须在drawRect 方法中才可以调用。也就是说你需要在这个drawRect方法中来完成你的绘制。
而你的做法是将绘制与touch的动作建立了紧密的联系。其实你是可以参考zhangao的做法,在touch事件中只需要处理你在绘制中感兴趣的东西,就是touch的点(起始点,终止点)得到这些点,再通过[self setNeedsDisplay] 让drawRect 进行绘制。[/quote]
I got it.
1. 做动作的时候, 记录划过的点, 并设定相关标志。然后指定重绘
2. 重绘时根据这些信息来绘制。
[/quote]
不要在drawRect里重画,不然你得把touchBegin和touchEnd记录到path里去,path限制主要有两点:做不了橡皮擦(我暂时没找到方法);虽然能放到CALayer里去(避免每次重绘),但是需要每个子类去创建CALayer(CALayer只能对应CGPath,无法对应CGContext),无法进行良好的封装。
我放在GitHub上的代码在持续更新,已经加入了橡皮擦等工具,还在找能够提升效率的方法
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
把context保存到image里[/quote]
1. 你这个函数是在drawRect里面调用的嘛?
因为当你在
drawRect里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,UIGraphicsBeginImageContext返回的上下文是空的,
但是你若是在touchedMoved里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,在UIGraphicsBeginImageContext代码被调用之前,此时的当前上下文是什么?[/quote]
CGContextRef context = UIGraphicsGetCurrentContext();这个绘制的上下文是必须在drawRect 方法中才可以调用。也就是说你需要在这个drawRect方法中来完成你的绘制。
而你的做法是将绘制与touch的动作建立了紧密的联系。其实你是可以参考zhangao的做法,在touch事件中只需要处理你在绘制中感兴趣的东西,就是touch的点(起始点,终止点)得到这些点,再通过[self setNeedsDisplay] 让drawRect 进行绘制。[/quote]
I got it.
1. 做动作的时候, 记录划过的点, 并设定相关标志。然后指定重绘
2. 重绘时根据这些信息来绘制。
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
把context保存到image里[/quote]
1. 你这个函数是在drawRect里面调用的嘛?
因为当你在
drawRect里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,UIGraphicsBeginImageContext返回的上下文是空的,
但是你若是在touchedMoved里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,在UIGraphicsBeginImageContext代码被调用之前,此时的当前上下文是什么?[/quote]
当然不是在drawRect里,之前已经说了,是在自定义方法里,默认是没有上下文的,要自己通过UIGraphicsBeginImageContext开启一个新的[/quote]
Hi, 参考你的回复。我使用了如下代码,可以实现基本的功能。
思想是这样...
1. 建立一成员变量UIImage
2. 第一次绘制时
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(12, 12, 300, 500); //_beginPoint.x, _beginPoint.y, _endPoint.x - _beginPoint.x, _endPoint.y - _beginPoint.y);
// 实心矩形
CGRect rc1 = rect;
rc1.origin.x += 4;
rc1.origin.y += 4;
rc1.size.width -= 8;
rc1.size.height -= 8;
CGContextSetLineWidth(ctx, 5);
CGContextSetRGBFillColor(ctx, 0.5, 0.2, 0, 1);
CGContextFillRect(ctx, rc1);
CGContextStrokePath(ctx);
// …
m_pCacheImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
绘制矩形A.
2. 第二次绘制时:
- (void)test2 {
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[m_pCacheImage drawAtPoint:CGPointMake(0, 0)];
CGRect rect = CGRectMake(512, 312, 200, 200); //_beginPoint.x, _beginPoint.y, _endPoint.x - _beginPoint.x, _endPoint.y - _beginPoint.y);
// 实心矩形
CGRect rc1 = rect;
rc1.origin.x += 4;
rc1.origin.y += 4;
rc1.size.width -= 8;
rc1.size.height -= 8;
CGContextSetLineWidth(ctx, 5);
CGContextSetRGBFillColor(ctx, 0.5, 0.2, 0, 1);
CGContextFillRect(ctx, rc1);
CGContextStrokePath(ctx);
// …
m_pCacheImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setNeedsDisplay];
}
构建一个上下文,将当前的UIImage先绘制上去,然后再绘制本次需要绘制的内容,完成后再刷新
3. drawRect里面只要简单的
- (void)drawRect:(CGRect)rect
{
if( m_pCacheImage ) {
[m_pCacheImage drawAtPoint:CGPointMake(0, 0)];
}
}
就ok了。。。。
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
把context保存到image里[/quote]
1. 你这个函数是在drawRect里面调用的嘛?
因为当你在
drawRect里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,UIGraphicsBeginImageContext返回的上下文是空的,
但是你若是在touchedMoved里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,在UIGraphicsBeginImageContext代码被调用之前,此时的当前上下文是什么?[/quote]
CGContextRef context = UIGraphicsGetCurrentContext();这个绘制的上下文是必须在drawRect 方法中才可以调用。也就是说你需要在这个drawRect方法中来完成你的绘制。
而你的做法是将绘制与touch的动作建立了紧密的联系。其实你是可以参考zhangao的做法,在touch事件中只需要处理你在绘制中感兴趣的东西,就是touch的点(起始点,终止点)得到这些点,再通过[self setNeedsDisplay] 让drawRect 进行绘制。
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
把context保存到image里[/quote]
1. 你这个函数是在drawRect里面调用的嘛?
因为当你在
drawRect里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,UIGraphicsBeginImageContext返回的上下文是空的,
但是你若是在touchedMoved里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,在UIGraphicsBeginImageContext代码被调用之前,此时的当前上下文是什么?[/quote]
当然不是在drawRect里,之前已经说了,是在自定义方法里,默认是没有上下文的,要自己通过UIGraphicsBeginImageContext开启一个新的
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
把context保存到image里[/quote]
1. 你这个函数是在drawRect里面调用的嘛?
因为当你在
drawRect里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,UIGraphicsBeginImageContext返回的上下文是空的,
但是你若是在touchedMoved里面调用
UIGraphicsBeginImageContext(self.bounds.size)
// …
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
的时候,在UIGraphicsBeginImageContext代码被调用之前,此时的当前上下文是什么?