2012-01-05 57 views
3

我正在使用此函数来检测UIImageView的触摸点是否与透明像素相对应。在UIImageView中检测转换图像上的Alpha通道

-(BOOL)isTransparency:(CGPoint)point 
{ 
    CGImageRef cgim = self.image.CGImage; 

    unsigned char pixel[1] = {0}; 

    point.x = (point.x/(self.frame.size.width/CGImageGetWidth(cgim))); 
    point.y = (point.y/(self.frame.size.height/CGImageGetHeight(cgim))); 


    CGContextRef context = CGBitmapContextCreate(pixel, 
               1, 1, 8, 1, NULL, 
               kCGImageAlphaOnly); 

    CGContextSetBlendMode(context, kCGBlendModeCopy); 

    CGContextDrawImage(context, CGRectMake(-point.x, 
              -(self.image.size.height - point.y), 
              CGImageGetWidth(cgim), 
              CGImageGetHeight(cgim)), 
         cgim); 

    CGContextRelease(context); 

    CGFloat alpha = pixel[0]/255.0; 

    return alpha < 0.01; 

} 

如果UIImageView转换没有被修改,可以正常工作。但是当用户旋转图像时,变换矩阵会被修改,这段代码会将图像置于原始位置,而不是对应于变换后的图像位置,并且我会得到错误的结果。

我的问题是如何检测UIImageView的图像的触摸像素在旋转时是否透明?

谢谢。

编辑: -Version 1.1〜

嗨。感谢你的回答。我已经修改了代码,现在这个版本仍然工作作为以前的版本:

-(BOOL)isTransparency:(CGPoint)point 
{ 
    CGImageRef cgim = self.image.CGImage; 

    NSLog(@"Image Size W:%lu H:%lu",CGImageGetWidth(cgim),CGImageGetHeight(cgim)); 

    unsigned char pixel[1] = {0}; 

    point.x = (point.x/(self.frame.size.width/CGImageGetWidth(cgim))); 
    point.y = (point.y/(self.frame.size.height/CGImageGetHeight(cgim))); 


    CGContextRef context = CGBitmapContextCreate(pixel, 
               1, 1, 8, 1, NULL, 
               kCGImageAlphaOnly); 

    CGContextSetBlendMode(context, kCGBlendModeCopy); 

    CGContextTranslateCTM(context, self.image.size.width * 0.5f, self.image.size.height * 0.5f); 
    CGContextConcatCTM(context, self.transform); 
    //CGContextScaleCTM(context, [self currentPercent]/100, [self currentPercent]/100); 
    //CGContextRotateCTM(context, atan2f(self.transform.b, self.transform.a)); 
    CGContextTranslateCTM(context, -self.image.size.width * 0.5f, -self.image.size.height * 0.5f); 

    CGContextDrawImage(context, CGRectMake(-point.x, 
              -(self.image.size.height - point.y), 
              self.image.size.width, 
              self.image.size.height), 
         cgim); 

    CGContextRelease(context); 

    CGFloat alpha = pixel[0]/255.0; 

    NSLog(@"Is Transparent: %d",alpha < 0.01); 

    return alpha < 0.01; 

} 

我有几个函数试图旋转和原始图像缩放到当前的旋转和尺寸都没有成功。 :(

任何帮助,请

编辑:? 1.2版

我发现了一个变通方法来解决这个问题,我决定创建大小等于屏幕尺寸的情况下,绘制只有感动对象,与当前变换矩阵根据。

之后创建此上下文其中图像被适当地绘制和图像传递到仅与非转化的图像的工作原理的功能的位图。

结果是,工作正常。我不知道这是否是最佳的方式,但工作。

这里的代码:

-(BOOL)isTransparency:(CGPoint)point 
{ 
    UIGraphicsBeginImageContext(self.superview.bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSaveGState(context); 

    CGContextSetBlendMode(context, kCGBlendModeCopy); 

    CGContextTranslateCTM(context, [self center].x, [self center].y); 
    CGContextConcatCTM(context, [self transform]); 
    CGContextTranslateCTM(context, 
          -[self bounds].size.width * [[self layer] anchorPoint].x, 
          -[self bounds].size.height * [[self layer] anchorPoint].y); 

    [[self image] drawInRect:[self bounds]]; 

    CGContextRestoreGState(context); 

    CGImageRef image = CGBitmapContextCreateImage(context); 

    CGImageRef viewImage = image; 

    return [self isTransparentPixel:point image:image]; 

} 

该函数创建与上海华的尺寸的图像(在我的情况总是充满屏幕),并且接收作为参数UIKit中的触摸点从父视图的坐标系统。

-(BOOL)isTransparentPixel:(CGPoint)point image:(CGImageRef)cgim 
{ 
    NSLog(@"Image Size W:%lu H:%lu",CGImageGetWidth(cgim),CGImageGetHeight(cgim)); 

    unsigned char pixel[1] = {0}; 

    CGContextRef context = CGBitmapContextCreate(pixel, 
               1, 1, 8, 1, NULL, 
               kCGImageAlphaOnly); 

    CGContextSetBlendMode(context, kCGBlendModeCopy); 

    CGContextDrawImage(context, CGRectMake(-point.x, 
              -(self.superview.frame.size.height - point.y), 
              CGImageGetWidth(cgim), 
              CGImageGetHeight(cgim)), 
         cgim); 

    CGContextRelease(context); 

    CGFloat alpha = pixel[0]/255.0; 

    return alpha < 0.01; 

} 

该函数检测给定图像中给定像素的alpha通道。

谢谢大家。

回答

2

在绘制图像之前,使用诸如CGContextConcatCTM()之类的函数将图像视图中的变换应用到CGContext。

+0

我试过但没有工作,在这个问题中,我用Concat,Rotate和Scale函数编辑了函数的版本,但没有成功。 – NemeSys 2012-01-11 20:26:56

+0

如果图像的大小与图像大小不同,您可能需要将它移动到图像视图大小的中心位置。现在,可以更容易地将图像视图的全部大小绘制到上下文中,并将该CGImage写入磁盘,或将其显示在新视图中,以确保正确应用转换。 – 2012-01-12 01:30:59

相关问题