0

我正在处理图像编辑应用程序。现在我已经构建了应用程序,用户可以从他们的图书馆中选择一张照片或者使用相机拍摄照片。我还有另一个视图(一个选择器视图),用户可以从中选择其他图像。通过选择其中一个图像,应用程序将用户带回主照片。如何通过触摸将图像添加到视图?

我希望用户能够触摸屏幕上的任何位置并添加他们选择的图像。

解决此问题的最佳方法是什么?

touchesBegan? touchesMoved? UITapGestureRecognizer?

如果有人知道任何示例代码,或者可以给我一个关于如何处理这个问题的大概想法,那将非常棒!

编辑

现在我能看到的坐标,而我的UIImage越来越从我选择器选择图像。但是当我点击时图像没有显示在屏幕上。有人可以帮助我解决我的代码,请:

-(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

在您的ViewController中,用户使用方法“ - (void)touchesBegan:(NSSet *)与事件触发:(UIEvent *)事件”来获取触摸发生位置的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 ==无”。如果这是真的,您的UIImage可能没有正确创建。

+0

我在更彻底地重新阅读您的问题后更新了我的答案。 – bddicken 2012-07-17 03:59:47

+0

谢谢!我正在处理你的新答案。它帮了大忙! – 2012-07-17 04:54:01

相关问题