尝试使用触摸事件在另一个图像上绘制图像 [英] Attempting to draw image on top of another image using a touch event

查看:84
本文介绍了尝试使用触摸事件在另一个图像上绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个名为mainvie的视图 - 当应用程序运行时,加载了主视图,将背景图像加载到屏幕上(下面的代码),ring_large.jpg已添加为文件。

in my application, I have a view named mainvie - when the application runs, mainview is loaded which loads a background image onto the screen ( code below) ring_large.jpg has been added as a file.

- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"rink_large.jpg"];
CGPoint imagepoint = CGPointMake(10,0); 
[image drawAtPoint:imagepoint];
}

这个工作正常,就在我试图在顶部绘制另一个图像的时候这是我有问题。其他地方(文件名为mainviewcontroller.m) - 触摸即使我试图获取触摸的位置,并在该位置绘制图像。下面列出的是我的代码。我不知道为什么我要放置的图像根本就没有绘制。我确信它不是在溜冰场图像后面,因为我评论了这一点,并且图像在点击时仍然没有绘制。这是触摸开始函数,它应该绘制图像。

This works fine, it is when I am trying to draw another image on top of this that I am having issues. Elsewhere (file named mainviewcontroller.m) - On a touch even I am trying to get the location of the touch, and draw an image at the location. Listed below is my code. I am not sure why the image I am trying to place is not drawing at all. I am sure it is not drawing behind the rink image, as i commented this out and the image still doesnt draw when clicked. Here is the touches begin function which should draw the image.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     CGPoint location = [[touches anyObject] locationInView:mainView];
    UIImage *image = [UIImage imageNamed:@"small_cone.png"];
    [image drawAtPoint:location];
}

任何人都可以看到为什么图像在某处被触摸时不会被绘制? touchesBegan功能在任何地方触摸屏幕时开始,但图片不显示。感谢您的帮助,我比较新的目标 - c。

Can anyone see why the image will not draw when somewhere is touched? The touchesBegan function start when the screen is touched anywhere, but the picture is not displaying. Thanks for your help, I am newer to objective - c.

推荐答案

UIImage drawAtPoint在当前图形上下文中绘制图像。您没有定义图形上下文。在drawRect(原始代码所在的位置)中,已经存在图形上下文。基本上,你是在告诉UIImage要绘制的位置,而不是在上绘制的内容。

UIImage drawAtPoint draws the image within the current graphics context. You are not defining a graphics context. In drawRect (where your original code is) there is already a graphics context. Basically, you are telling the UIImage what location to draw at, but not what to draw on.

你需要更像这样的东西:

You need something more like this:

CGPoint location = [[touches anyObject] locationInView:mainView];

UIGraphicsBeginImageContext(mainView.bounds.size);
UIImage *image = [UIImage imageNamed:@"small_cone.png"];
[image drawAtPoint:location];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但是,这不会保留或考虑您的原始图像。如果你想要它们都被绘制,一个在另一个上面,对两个图像使用drawAtPoint:

However, this won't preserve or take into account your original image. If you want them both drawn, one over the other, use drawAtPoint for both images:

CGPoint location = [[touches anyObject] locationInView:mainView];

UIGraphicsBeginImageContext(mainView.bounds.size);

UIImage *image = [UIImage imageNamed:@"rink_large.jpg"];
CGPoint imagepoint = CGPointMake(10,0);
[image drawAtPoint:imagepoint];

image = [UIImage imageNamed:@"small_cone.png"];
[image drawAtPoint:location];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

现在你可以用newImage做一些事情,它包含两个图像的合成。

Now you can do something with newImage, which contains a composite of both images.

这篇关于尝试使用触摸事件在另一个图像上绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆