我正在开发一个图片编辑应用。现在我已经构建了应用程序,因此用户可以从他们的库中选择一张照片或者用相机拍照。我还有另一个视图(选择器视图),其中包含用户可以选择的其他图像。通过选择其中一个图像,应用程序将用户带回主照片。
我希望用户能够触摸屏幕上的任意位置并添加他们选择的图像。
最好的方法是什么?
的touchesBegan? touchesMoved? UITapGestureRecognizer?
如果有人知道任何示例代码,或者可以让我大致了解如何处理这个问题,那就太棒了!
修改:
现在我能够看到坐标,我的UIImage正在获取我从Picker中选择的图像。但是当我点击时,图像没有显示在屏幕上。有人可以帮我解决我的代码问题:
-(void)drawRect:(CGRect)rect
{
CGRect currentRect = CGRectMake(touchPoint.x, touchPoint.y, 30.0, 30.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context, currentRect);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
touchPoint = [touch locationInView:imageView];
NSLog(@"%f", touchPoint.x);
NSLog(@"%f", touchPoint.y);
if (touchPoint.x > -1 && touchPoint.y > -1)
{
stampedImage = _imagePicker.selectedImage;
//[stampedImage drawAtPoint:touchPoint];
[_stampedImageView setFrame:CGRectMake(touchPoint.x, touchPoint.y, 30.0, 30.0)];
[_stampedImageView setImage:stampedImage];
[imageView addSubview:_stampedImageView];
NSLog(@"Stamped Image = %@", stampedImage);
//[self.view setNeedsDisplay];
}
}
我看到的NSLogs示例:
162.500000
236.000000
Stamped Image = <UIImage: 0xe68a7d0>
谢谢!
答案 0 :(得分:0)
在ViewController中,用户方法“ - (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)event”以获取触摸发生位置的X和Y坐标。以下是一些示例代码,演示了如何获取触摸的x和y
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
/* Detect touch anywhere */
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(@"%f", touchPoint.x); // The x coordinate of the touch
NSLog(@"%f", touchPoint.y); // The y coordinate of the touch
}
获得此x和y数据后,您可以将用户选择或使用内置摄像头拍摄的图像设置为显示在这些坐标上。
编辑:
我认为问题可能在于如何创建UIImage视图。而不是:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
touchPoint = [touch locationInView:imageView];
CGRect myImageRect = CGRectMake(touchPoint.x, touchPoint.y, 20.0f, 20.0f);
UIImageView * myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:_stampedImageView.image];
myImage.opaque = YES;
[imageView addSubview:myImage];
[myImage release];
}
试试这个:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
touchPoint = [touch locationInView:imageView];
myImage = [[UIImageView alloc] initWithImage:_stampedImageView.image];
[imageView addSubview:myImage];
[myImage release];
}
如果这不起作用,请尝试检查“_stampedImageView.image == nil”。如果是这样,可能无法正确创建您的UIImage。