我正在使用此函數來檢測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通道。
謝謝大家。
我試過但沒有工作,在這個問題中,我用Concat,Rotate和Scale函數編輯了函數的版本,但沒有成功。 – NemeSys 2012-01-11 20:26:56
如果圖像的大小與圖像大小不同,您可能需要將它移動到圖像視圖大小的中心位置。現在,可以更容易地將圖像視圖的全部大小繪製到上下文中,並將該CGImage寫入磁盤,或將其顯示在新視圖中,以確保正確應用轉換。 – 2012-01-12 01:30:59