0

下面的代码在 ARC 中工作正常,但在非 arc 中不起作用,实际上我想在非 arc 代码中实现这个自由手绘。非弧线的问题是指针在一个地方,而线条在另一个地方绘制。这是供您
参考的屏幕截图

代码:在.h

UIImageView *drawImage;
CGPoint location;
CGPoint lastPoint;
CGPoint moveBackTo;
CGPoint currentPoint;
NSDate *lastClick;
BOOL mouseSwiped;
NSMutableArray *latLang;
  ///
- (void)viewDidLoad
{
[super viewDidLoad];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
drawImage.image = [defaults objectForKey:@"drawImageKey"];
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:drawImage];
drawImage.backgroundColor = [UIColor blueColor];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];

if ([touch tapCount] == 2) {
    drawImage.image = nil;
}


location = [touch locationInView:self.view];
lastClick = [NSDate date];

lastPoint = [touch locationInView:self.view];
NSLog(@"LastPoint:%@",NSStringFromCGPoint(lastPoint));
lastPoint.y -= 0;
mouseSwiped = YES;
[super touchesBegan: touches withEvent: event];


}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height));
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 8.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());


[drawImage setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (mouseSwiped) {

}
lastPoint = currentPoint;

[self.view addSubview:drawImage];
}
4

1 回答 1

0

我想问题在于lastClick的自动释放。最简单的事情可以更换:

NSDate *lastClick;

@property (strong, nonatomic) NSDate *lastClick;

...好吧,我混淆了ARC,没有ARC。由于您没有 ARC,因此您必须保留日期:

lastClick = [[NSDate date] retain];

然后为 touchesEnd 添加处理程序并实现:

[lastClick release];
lastClick = nil;
于 2013-09-23T14:32:11.890 回答