0

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

4

2 回答 2

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 上)。

于 2013-09-30T10:58:54.163 回答
0

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

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

通过使用此示例解决了所有问题

于 2013-10-01T12:19:54.673 回答