【问题标题】:EXC_BAD_ACCESS in drawRect:drawRect 中的 EXC_BAD_ACCESS:
【发布时间】:2013-09-30 10:01:27
【问题描述】:

我在 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

【问题讨论】:

  • 你似乎在drawRect外部绘图?
  • 当我在 UIView 上移动手指时必须进行绘制
  • 然后你使视图无效以强制重绘,你不只是开始绘图。见[UIView setNeedsDisplay]
  • incrementalImage 是什么类型的?它在哪里实例化?你期望在这里发生什么?你所做的只是强制一些其他对象(可能也是 UIView 子类?)来绘制自己,你可能会强制它进入一个可能不是它自己的绘图区域的矩形。
  • 那里有[incrementalImage drawAtPoint:CGPointZero]; 是否正确?它甚至有意义吗?

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


【解决方案1】:
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 上)。

【讨论】:

  • 使用您的解决方案,我在 drawInRect 中遇到错误。错误和第一个类似
  • 用我的解决方案你不需要使用drawInRect:。 UIImageView 绘制图像没有任何添加。您只需要从上下文中收集绘图并将其交给它。
  • 在您的解决方案中,第三行有 drawInRect
  • 您的内存管理似乎有误。尝试在您尝试绘制时检查变量是否存在。
【解决方案2】:

对于在 ios 中徒手绘制有一个很好的例子:

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

用这个例子解决了所有问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多