For apple said that we can’t use drawRec: in a UIImageView. so I decide to use CALayer to do the work, and add the layer to the UIImageView.layer.
I made a Class named MyCALayer extends the CALayer class, and I overwrite the drawInContex: method with:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,5.0f);
CGContextSetStrokeColorWithColor(context,[UIColor yellowColor].CGColor);
CGRect newRec = CGRectMake(55,55,55,55);
CGContextAddRect(context,newRec);
CGContextDrawPath(context,kCGPathStroke);
And I used the setNeedsDisplay in touchesBegan:withEvent to make the drawInContext: could be called.
Each logic which uses context (such as CGContextSetLineWidth(context,5.0f);) get a null- handle warning code like:
Error:CGContextSetWidth: invalid context 0x0
So, Wasn’t the setNeedsDisplay create the context I needed?
You are supposed to draw to the context that is passed as an argument to the
drawInContext:method. There is no need to callUIGraphicsGetCurrentContext().