我想在imageview上标记一些东西并且必须存储在数据库中,当我将从数据库中检索它时,我必须在imageview上显示相同的坐标。
答案 0 :(得分:1)
您在故事板(或代码中)中创建了UIImageView
,并在viewController
中创建了fowling属性:
@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;
接下来,在viewDidLoad
初始化tapGesture:
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
self.tapGesture.delegate = self;
[self.view addGestureRecognizer:self.tapGesture];
接下来,为tap:
创建处理程序- (void)handleSingleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer {
CGPoint point = [tapGestureRecognizer locationInView:self.imageView];
float squareSize = 10;
UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, YES, 0);
[self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point.x-squareSize, point.y - squareSize);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x+squareSize, point.y-squareSize);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x+squareSize, point.y+squareSize);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x-squareSize, point.y+squareSize);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x-squareSize, point.y-squareSize);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0,0,0,1);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
[self.imageView setAlpha:1.0];
UIGraphicsEndImageContext();
}
此代码在imageView上绘制一个正方形。现在,您可以将point
保存在数据库中,并在需要时显示图像绘制方形标记。
我希望这对你有所帮助。