drawRect中的EXC_BAD_ACCESS:

时间:2013-09-30 10:01:28

标签: ios objective-c uiview compiler-errors exc-bad-access

我将UIView置于WebView上方。在这个UIView我必须画画,但应用程序崩溃与此错误

LibraryViewController.m中的

 self.drawView =[[DrawView alloc]init];
 self.drawView.frame=CGRectMake(0, 0, 1024, 768);
 self.drawView.backgroundColor=[UIColor redColor];
 self.drawView.alpha=0.5;

 [self.webView addSubview:self.drawView];

在DrawView.h中

 #import <UIKit/UIKit.h>

 @interface DrawView : UIView

 @end

在DrawView.m

@implementation DrawView
{
   UIImage *incrementalImage;
   //......
}

 - (void)eraseDrawing:(UITapGestureRecognizer *)t
{
    incrementalImage = nil;
    [self setNeedsDisplay];
}

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
         //.....
        [incrementalImage drawAtPoint:CGPointZero];
        [[UIColor blackColor] setStroke];
        [[UIColor blackColor] setFill];
        [offsetPath stroke]; // ................. (8)
        [offsetPath fill]; 
        incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [offsetPath removeAllPoints];
        dispatch_async(dispatch_get_main_queue(), ^{
            bufIdx = 0;
            [self setNeedsDisplay];
           });
       });
       //.....
  }

 - (void)drawRect:(CGRect)rect
 {
      [incrementalImage drawInRect:rect]; //Thread 1:EXC_BAD_ACCESS (code=1 address=0xa000008)
 }

此应用程序不使用ARC

2 个答案:

答案 0 :(得分:0)

UIGraphicsBeginImageContext(self.photoEditView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.photoEditView.image drawInRect:CGRectMake(0, 0, self.photoEditView.frame.size.width, self.photoEditView.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 20.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, lastPoint.x, lastPoint.y);

CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGPathRelease(path);
self.photoEditView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

此代码适用于我(即使在iOS 7上)。

答案 1 :(得分:0)

对于ios中的徒手画,有一个很好的例子:

https://github.com/levinunnink/Smooth-Line-View

使用此示例

解决了所有问题