【问题标题】:How do I add images to a view by touch?如何通过触摸将图像添加到视图中?
【发布时间】:2012-07-17 00:23:09
【问题描述】:

我正在开发一个图像编辑应用程序。现在我已经构建了应用程序,因此用户可以从他们的库中选择一张照片或用相机拍照。我还有另一个视图(选择器视图),其中包含用户可以选择的其他图像。通过选择其中一张图像,应用程序会将用户带回主照片。

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

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

触摸开始了吗?感动了吗? 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];
    }
}

以我看到的 NSLog 为例:

162.500000
236.000000
Stamped Image = <UIImage: 0xe68a7d0>

谢谢!

【问题讨论】:

    标签: objective-c uiimageview uitouch touchesbegan uitapgesturerecognizer


    【解决方案1】:

    在您的 ViewController 中,使用方法“-(void)touchesBegan:(NSSet *)touches 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 可能没有正确创建。

    【讨论】:

    • 在更彻底地重新阅读了您的问题后,我更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多