IOS WKWebView gets RGBA pixel color from dot

How can I get the color of an RGBA pixel pixel from a WKWebView?

I have a working solution for UIWebView, but I would like to use WKWebView. When I click on a point on the screen, I can get the color value in RGBA from the UIWebView, for example (0,0,0,0), when it is transparent or something like (0,76,0,23,0,34,1 ) when it is not transparent. WKWebView always returns (0,0,0,0) instead.

More details

I am working on an iOS application as WebView is the biggest ui element.

WebView has transparent areas so you can see the underlying UIView.

WebView should ignore strokes on transparent areas, and the original UIView should receive this event.

So I redefined the hitTest function:

#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 

My guess is that WKWebView has another CALayer or hidden UIView where it draws the actual web page.

+7
ios objective-c pixel rgba wkwebview
source share
1 answer
 #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 

This code fixed my problem.

+5
source share

All Articles