美文网首页
适配wkwebview字符串显示,查看大图

适配wkwebview字符串显示,查看大图

作者: 马铃薯蜀黍 | 来源:发表于2023-06-25 16:52 被阅读0次

NSString * foreString = @"<html><head><meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <style type="text/css"> body { line-height:150%} </style>
<style>img{max-width: 100%; width:auto; height:auto;}</style><style>iframe {display: block;max-width:100%;
margin-top:10px; margin-bottom:10px;}</style><style type="text/css">
</style></head><body style="margin:0;padding:0">";
NSString * tailString = @"</body></html>";
NSString * htmlString = [NSString stringWithFormat:@"%@%@%@",foreString,htmlString,tailString];
[self.webView loadHTMLString:htmlString baseURL:nil];
[self.webView addTapImageGesture];//查看大图

=======================================

import <WebKit/WebKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface WKWebView (images)

-(void) addTapImageGesture;

@end

NS_ASSUME_NONNULL_END

import "WKWebView+images.h"

@implementation WKWebView (images)

-(void) addTapImageGesture
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.delegate = (id)self;
[self addGestureRecognizer:tapGesture];
}
//这里增加手势的返回,不然会被WKWebView拦截

  • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
    return YES;
    }
    -(void) tapGestureAction:(UITapGestureRecognizer *)recognizer
    {
    //首先要获取用户点击在WKWebView 的范围点
    CGPoint touchPoint = [recognizer locationInView:self];
    NSString *imgURLJS = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
    //跟着注入JS 获取 异步获取结果
    [self evaluateJavaScript:imgURLJS completionHandler:^(id result, NSError * _Nullable error) {
    if (error == nil)
    {
    NSString *url = result;
    if (url.length == 0)
    {
    return ;
    }
    else
    {
    //如果是url 则转换成 UIImage
    // NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];

              NSLog(@"dddddd = %@", url);
    

// UIImage *clickImg = [UIImage imageWithData:imgData];
// if (clickImg)
// {
//
//
// }
NSArray *imgArr = @[url];
// NSMutableArray *tempArray = [[NSMutableArray alloc] init];
// [tempArray addObject:clickImg];

        //TO对图片的操作
            NSMutableArray * bigImageArray = [NSMutableArray array];
            for (int i = 0; i < imgArr.count; i ++) {
                YBIBImageData *data = [YBIBImageData new];
                data.imageURL = [NSURL URLWithString:imgArr[i]];
                data.allowSaveToPhotoAlbum = YES;

// data.defaultLayout.zoomScaleSurplus = 3;
[bigImageArray addObject:data];
}

            //    [bigImageArray addObject:data];
            
            YBImageBrowser *browser = [YBImageBrowser new];
            
            browser.dataSourceArray = bigImageArray;
            browser.currentPage = 0;
            [browser show];
            
        }
    }
}];

}

@end

相关文章

网友评论

      本文标题:适配wkwebview字符串显示,查看大图

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