大家好,我的 iOS 应用需要帮助。如何在我点击的位置获取图像的背景颜色(我在 UIImageView 中插入)。我创建了一个点击手势识别器,但我不知道如何读取我点击的背景颜色。
小智 5
在您的控件中添加手势
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
[lblSlider addGestureRecognizer:tapRecognizer];
lblSlider.userInteractionEnabled = YES;
Run Code Online (Sandbox Code Playgroud)
添加此方法后
- (void)tapGesture:(UITapGestureRecognizer *)recognizer
{
CGPoint point1 = [recognizer locationInView:recognizer.view];
UIGraphicsBeginImageContext(recognizer.view.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[recognizer.view.layer renderInContext:context];
int bpr = (int)CGBitmapContextGetBytesPerRow(context);
unsigned char * data = CGBitmapContextGetData(context);
if (data != NULL)
{
int offset = bpr*round(point1.y) + 4*round(point1.x);
int blue = data[offset+0];
int green = data[offset+1];
int red = data[offset+2];
int alpha = data[offset+3];
NSLog(@"%d %d %d %d", alpha, red, green, blue);
if (alpha == 0)
{
// Here is tap out of text
}
else
{
// Here is tap right into text
}
}
UIGraphicsEndImageContext();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
193 次 |
| 最近记录: |