iOS WKWebView 从点获取 RGBA 像素颜色

标签 ios objective-c pixel rgba wkwebview

如何从 WKWebView 获取点的 RGBA 像素颜色?

我有一个适用于 UIWebView 的可行解决方案,但我想改用 WKWebView。当我点击屏幕上的某个点时,我能够从 UIWebView 中检索 RGBA 中的颜色值,例如 (0,0,0,0) 当它是透明的或类似 (0.76,0.23,0.34,1) 时它不透明。 WKWebView 始终返回 (0,0,0,0)。

更多详情

我正在开发一个将 WebView 作为最顶级 ui 元素的 iOS 应用。

WebView 具有透明区域,因此您可以看到底层的 UIView。

WebView 应忽略对透明区域的触摸,底层 UIView 应检索该事件。

因此我重写了 hitTest 函数:

#import "OverlayView.h"
#import <QuartzCore/QuartzCore.h>

@implementation OverlayView

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {

    UIView* subview = [super hitTest:point withEvent:event];  // this will always be a webview

    if ([self isTransparent:point fromView:subview.layer]) // if point is transparent then let superview deal with it
    {
        return [self superview];
    }

    return subview; // return webview
}

- (BOOL) isTransparent:(CGPoint)point fromView:(CALayer*)layer
{
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [layer renderInContext:context];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return (pixel[0]/255.0 == 0) &&(pixel[1]/255.0 == 0) &&(pixel[2]/255.0 == 0) &&(pixel[3]/255.0 == 0) ;
}

@end

我的假设是 WKWebView 有一个不同的 CALayer 或一个隐藏的 UIView,它在其中绘制实际的网页。

最佳答案

#import "OverlayView.h"
#import <QuartzCore/QuartzCore.h>

@implementation OverlayView

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {

    UIView* subview = [super hitTest:point withEvent:event];  // this should always be a webview

    if ([self isTransparent:[self convertPoint:point toView:subview] fromView:subview.layer]) // if point is transparent then let superview deal with it
    {
        return [self superview];
    }

    return subview; // return webview
}

- (BOOL) isTransparent:(CGPoint)point fromView:(CALayer*)layer
{
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y );

    UIGraphicsPushContext(context);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIGraphicsPopContext();

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return (pixel[0]/255.0 == 0) &&(pixel[1]/255.0 == 0) &&(pixel[2]/255.0 == 0) &&(pixel[3]/255.0 == 0) ;
}

@end

这段代码解决了我的问题。

关于iOS WKWebView 从点获取 RGBA 像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31038306/

相关文章:

ios - 防止 Realm 写入 JSON 两次

ios - 火力地堡 ".read": "auth != null && auth.uid == $uid" not working as expected

ios - 如果应用程序不在 iOS 的前台或后台,如何显示推送通知的警报 View ?

ios - 如何仅重新加载 UICollectionView 的可见部分?

c# - 如何转发触摸事件以在其下查看

ios - Facebook SDK 延迟深度链接在 iOS 14 中不起作用

ios - 如何访问自定义单元格元素

python - 使用 GDAL 将经度、纬度转换为像素值

c - 确定透明度的公式

javascript - 使用 JS 在 html/css 中将 px 值转换为 % 的代码?