ios - drawRect :中的EXC_BAD_ACCESS

我有一个放在WebView上方的UIView。在这个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

最佳答案

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上也是如此)。

https://stackoverflow.com/questions/19091382/

相关文章:

java - 找不到符号(扩展另一个类)

c# - C#在数学表达式中使用未分配的变量

java - 错误 :class interface or enum expected

java - 在其他类中访问主类中的公共(public)变量

c# - 编译内容项目时出错

java - 编写一个程序,询问产品代码,产品数量并汇总所有价格

c++ - C++中的运行时错误字符串声明

c++ - Count.h :10:5: error: ISO C++ forbids declar

java - 在PHP中获取Java编译错误

c++ - 没有匹配的函数用于调用c++