接触使用CGContext绘制的UIImage

时间:2012-02-23 15:23:56

标签: iphone ios xcode cocoa-touch ipad

我正在尝试检测用户是否使用CGContext和下面的路径点击绘图中的图像(这发生在名为SwbPlate的customview类中:

  SwbPlateImage * image;
    CGFloat startAngle;
    CGFloat endAngle;

    CGContextRef context = UIGraphicsGetCurrentContext();


    for (image in plateImages) {
        endAngle = [EntGeometry pointToRadian:[image endPoint] withCenterPoint:CGPointMake(self.bounds.size.width/2 , self.bounds.size.height /2)];
        startAngle = [EntGeometry pointToRadian:[image startPoint] withCenterPoint:CGPointMake(self.bounds.size.width/2 , self.bounds.size.height /2)];
        imageToDraw = [image plateImg];
        NSLog(@"%@", image);
        CGContextSaveGState(context);
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, self.bounds.size.width/2 , self.bounds.size.height/2);
        CGPathAddArc( path, 
                    NULL, 
                    self.bounds.size.width/2, 
                    self.bounds.size.height/2, 
                    (self.bounds.size.width/2),
                    endAngle,
                    startAngle,
                    0);
        CGPathCloseSubpath(path);

        CGContextAddPath(context, path);
        CGContextClip(context);

        CGContextTranslateCTM(context, 0, self.bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, rect, [imageToDraw CGImage]);
        CGContextRestoreGState(context);

        //RVE MOD
        image.imageContext = context;

然后我试着看看用户是否触摸了此上下文中的一个点,但这似乎不起作用

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];



    NSArray *allTouches = [touches allObjects]; 
UITouch *touch = [allTouches objectAtIndex:0];
SwbDragProduct*dragProd;





for (UIView *view in self.view.subviews) {

    if ([view isKindOfClass:[SwbPlateView class]] &(CGRectContainsPoint([view frame], [touch locationInView:self.view])) ) {
        NSLog(@" We touched in the PLATE");
        for (SwbPlateImage *imgView in plateView.plateImages){

            if(CGContextPathContainsPoint(imgView.imageContext, [touch locationInView:self.view],kCGPathFillStroke)){
                NSLog(@" We Touched within the IMAGE");
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

在我看来,你有SwbPlateImage局部变量,你在那里分配上下文,当你检查上下文时,for循环中没有这个上下文。您将需要具有管理和存储SwbPlateImage的上下文对象的Singleton类(例如SwbPlateImageSingleTon)。如果您可以提供有关您在问题中提到的类的更多详细信息,我可以帮助您编写单例类。

从你的问题来看,你需要将CGContextRef的NSMutableArray(比如contextArray)作为单例类中的强对象。不是将上下文分配给image.imageContext,而是将上下文添加到此数组中。

 [[SwbPlateImageSingleTon sharedInstance] contextArray] addObject:context]

然后你的内循环将如下所示。

   for (CGContextRef context in [[SwbPlateImageSingleTon sharedInstance] contextArray]) {
        if(CGContextPathContainsPoint(context, [touch locationInView:self.view],kCGPathFillStroke)) {
            NSLog(@" We Touched within the IMAGE");
        }
    }

Singleton类

SwbPlateImageSingleTon.h

@interface SwbPlateImageSingleTon : NSObject
  @property (strong, nonatomic) NSMutableArray *contextArray;
  + (SwbPlateImageSingleTon*)sharedInstance;
@end

SwbPlateImageSingleTon.m

#import "SwbPlateImageSingleTon.h"

@implementation SwbPlateImageSingleTon
static SwbPlateImageSingleTon *sharedObject = nil;

+ (SwbPlateImageSingleTon*)sharedInstance
{
    @synchronized(self) {
        if (!sharedObject) {
            sharedObject = [[SwbPlateImageSingleTon alloc] init];
        }
        return sharedObject;
    }
}
@end

答案 1 :(得分:1)

我正在寻找的是CGContextImage上的点击检测我发现,为了做我需要的检测,在CGContext图像上点击是不正确的方法。

我在内存中有一个点列表,我知道它们的坐标,为了检测uiscrollview上的点击,我迭代这些点以查看我点击的点是否在我已经绘制的图像的边界框内。这对我来说非常有效,我能够通过计算坐标和更新滚动视图的比例等来检测图像上的点击,以便将坐标转换为正确的比例。

所以简而言之,我得到的答案是,不可能让CGContext图像检测到水龙头,而是使用他们已知的位置来识别自己的命中。

由于 达明

答案 2 :(得分:1)

- (BOOL)pathContainsPoint:(NSPoint)point forMode:(CGPathDrawingMode)mode
{
CGPathRef path = [self quartzPath]; // Custom method to create a CGPath
CGContextRef cgContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGPoint cgPoint;
BOOL containsPoint = NO;

     cgPoint.x = point.x;
    cgPoint.y = point.y;

     // Save the graphics state before doing the hit detection.
    CGContextSaveGState(cgContext);

CGContextAddPath(cgContext, path);
containsPoint = CGContextPathContainsPoint(cgContext, cgPoint, mode);

     CGContextRestoreGState(cgContext);

     return containsPoint;
}