我有一个放在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/