发布网友 发布时间:2024-10-16 11:12
共1个回答
热心网友 时间:2024-11-06 04:35
- (BOOL)isTransparentAtPixel:(CGPoint)location withSpriteName:(NSString *)imageName { if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.boundingBox.size.width,self.boundingBox.size.height), location)) { NSLog(@"-------point -----------------------"); return YES; } // Create a 1x1 pixel byte array and bitmap context to draw the pixel into. NSInteger pointX = trunc(location.x); NSInteger pointY = trunc(location.y); NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; UIImage *image = [UIImage imageNamed:imageName];; CGImageRef cgImage = image.CGImage; NSUInteger width = image.size.width; NSUInteger height = image.size.height; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); int bytesPerPixel = 4; int bytesPerRow = bytesPerPixel * 1; NSUInteger bitsPerComponent = 8; unsigned char pixelData[4] = { 0, 0, 0, 0 }; CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextSetBlendMode(context, kCGBlendModeCopy); // Draw the pixel we are interested in onto the bitmap context CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height); CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage); CGContextRelease(context); // Convert color values [0..255] to floats [0.0..1.0] // CGFloat red = (CGFloat)pixelData[0] / 255.0f; // CGFloat green = (CGFloat)pixelData[1] / 255.0f; // CGFloat blue = (CGFloat)pixelData[2] / 255.0f; CGFloat alpha = (CGFloat)pixelData[3] / 255.0f; [pool release]; return (alpha == 0); }说明:1. 方法传入的两个参数 location和imageName,location为CCSprite内部的点坐标,imageName为CCSprite上的图片的名字。2.通过传入的imageName,新建一个UIImage,从而得到这个点的透明性,如果alpha返回为0,则表示该点是透明的。