我编写了以下代码片段。当用户在图像上点击两次(gesturerecognizer)时,它会添加注释。它成功添加了注释,但它并不完全是用户tapps的位置。点击点和注释图像位置之间存在一些偏移。
到目前为止,我无法弄清楚这一点。最有可能的是,我错过了一个我现在还不知道的详细信息。我在横向布局上使用ipad 2模拟器。
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.imvPhoto addGestureRecognizer:tapGesture];
tapGesture.delegate = self;
}
- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateRecognized) {
// handling code
CGPoint point = [sender locationInView:sender.view];
NSLog(@"Coordinates are %f, %f", point.x, point.y);
UIImage* anno=[UIImage imageNamed:@"annotation_icon.png"];
imvPhoto.image = [self drawImage:anno inImage:imvPhoto.image atPoint:point];
}
}
-(UIImage*) drawImage:(UIImage*) fgImage
inImage:(UIImage*) bgImage
atPoint:(CGPoint) point
{
UIGraphicsBeginImageContextWithOptions(bgImage.size, FALSE, 0.0);
[bgImage drawInRect:CGRectMake( 0, 0, bgImage.size.width, bgImage.size.height)];
[fgImage drawInRect:CGRectMake((point.x - fgImage.size.width/2), (point.y - fgImage.size.height/2), fgImage.size.width, fgImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
答案 0 :(得分:1)
您必须更改以下代码:
[fgImage drawInRect:CGRectMake(point.x, point.y, fgImage.size.width, fgImage.size.height)];
到
[fgImage drawInRect:CGRectMake((point.x - fgImage.size.width/2), (point.y - fgImage.size.height/2), fgImage.size.width, fgImage.size.height)];
答案 1 :(得分:0)
问题是我的背景图片大小是832乘603,我不知道。一旦图像被加载,它就会扩展到全屏,我认为它是1024 * 768。一旦我修正了图像的大小,问题就解决了。