ios - drawRect :中的EXC_BAD_ACCESS

标签 ios objective-c uiview compiler-errors 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上也是如此)。

关于ios - drawRect :中的EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19091382/

相关文章:

ios - 在 Objective C 代码中使用从 Objective C 库代码继承的 Swift 类

iphone - UIView的-hitTest :withEvent: called three times?

iphone - 从继承类调用方法

ios - (Swift) 将固定导航栏添加到 UICollectionView

ios - 在 objective-c 中进行字符串编码

ios - 改变场景cocos2d

iOS - 如何在 xcode 中解析 Json 数组并将结果保存为字符串

ios - 带有透明 UIImageView (iOS) 的页面 curl 动画

iphone - 在 Interface Builder 中构建 UIView

ios - App Store 评论和 Watchkit