美文网首页
从webView上获取图片的URL

从webView上获取图片的URL

作者: 梁苏珍 | 来源:发表于2017-12-11 16:55 被阅读0次

@interface ViewController ()@property (strong, nonatomic) UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];

[self.view addSubview:self.webView];

UILongPressGestureRecognizer *longPressed = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];

longPressed.delegate = self;

[self.webView addGestureRecognizer:longPressed];

}

- (void)longPressed:(UITapGestureRecognizer*)recognizer

{

if (recognizer.state != UIGestureRecognizerStateBegan) {

return;

}

CGPoint touchPoint = [recognizer locationInView:self.webView];

NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];

NSString *imageUrl = [self.webView stringByEvaluatingJavaScriptFromString:js];

if (imageUrl.length == 0) {

return;

}

NSLog(@"image url:%@",imageUrl);

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

UIImage *image = [UIImage imageWithData:data];

if (image) {

//长按的是web上的图片

//保存图片imageUrl

//......

//save image or Extract QR code

}

}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

相关文章

网友评论

      本文标题:从webView上获取图片的URL

      本文链接:https://www.haomeiwen.com/subject/aaziixtx.html