美文网首页
WKWebView 加载沙盒图片问题异常

WKWebView 加载沙盒图片问题异常

作者: Cocoa_Coder | 来源:发表于2020-03-31 14:24 被阅读0次

    记录一次异常,仅供参考

    出现异常的代码如下,WKWebView 通过 loadHTMLString:baseURL: 方法加载 其中baseURL参数地址为tmp/editor文件夹,包含了js,css文件

    
    <div id="article_content" contenteditable="true" autofocus="autofocus">
    
        <br>
    
            <img src="file:///private/var/mobile/Containers/Data/Application/64E9BAC5-ED7F-4D4C-A042-B6FC3A3376F2/tmp//EBC78615-07F1-4CEB-865E-19FE54F81AC1.jpg" alt="EBC78615-            07F1-4CEB-865E-19FE54F81AC1" style="width:100%;">
    
        <br>
    
        <img src="file:///private/var/mobile/Containers/Data/Application/64E9BAC5-ED7F-4D4C-A042-B6FC3A3376F2/tmp//A869DB48-5183-473C-A036-5252FA092B1B.jpg" alt="A869DB48-5183-473C-A036-5252FA092B1B" style="width:100%;">
    
        <br>
    
        <img src="file:///private/var/mobile/Containers/Data/Application/64E9BAC5-ED7F-4D4C-A042-B6FC3A3376F2/tmp//7E632178-4084-4F53-AEE8-53848AE267B0.jpg" alt="7E632178-4084-4F53-AEE8-53848AE267B0" style="width:100%;">
    
        <br>
    
        <img src="file:///private/var/mobile/Containers/Data/Application/64E9BAC5-ED7F-4D4C-A042-B6FC3A3376F2/tmp//F26D0FCE-3F5C-47BE-A808-09C65A3DD51C.jpg" alt="F26D0FCE-3F5C-47BE-A808-09C65A3DD51C" style="width:100%;">
    
        <br>
    
        <img src="file:///private/var/mobile/Containers/Data/Application/64E9BAC5-ED7F-4D4C-A042-B6FC3A3376F2/tmp//C9068776-DCAA-473E-9E06-6C96A8907990.jpg" alt="C9068776-DCAA-473E-9E06-6C96A8907990" style="width:100%;">
    
        <br>
    
        <img src="file:///private/var/mobile/Containers/Data/Application/64E9BAC5-ED7F-4D4C-A042-B6FC3A3376F2/tmp//9BCFF980-4DF5-46C2-A74A-D6E606F62D97.jpg" alt="9BCFF980-4DF5-46C2-A74A-D6E606F62D97" style="width:100%;">
    
        <br>
    
        <img src="file:///private/var/mobile/Containers/Data/Application/64E9BAC5-ED7F-4D4C-A042-B6FC3A3376F2/tmp//433FC88B-BE44-4C42-9178-6D60AB89BF9D.jpg" alt="433FC88B-BE44-4C42-9178-6D60AB89BF9D" style="width:100%;">
    
        <br>
    
        <img src="file:///private/var/mobile/Containers/Data/Application/64E9BAC5-ED7F-4D4C-A042-B6FC3A3376F2/tmp//A353D3D6-F1D7-4075-A7E0-17640053DD9E.jpg" alt="A353D3D6-F1D7-4075-A7E0-17640053DD9E" style="width:100%;">
    
        <br>
    
    </div>
    
    

    按理说不应该出现问题的,查看了下手机下的tmp文件夹,图片都有,就是加载不到,最后是这个原因

    
    OC代码:
    
    photos是图片选择器代理返回的所选图片
    
    [photos enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
            if(obj) {
    
                CGSizetargetSize = obj.size;
    
                if(obj.size.width>600)
    
                {
    
                    targetSize =CGSizeMake(600, obj.size.height/obj.size.width*600);
    
                }
    
                UIGraphicsBeginImageContext(targetSize);
    
                [objdrawInRect:CGRectMake(0,0,targetSize.width,targetSize.height)];
    
                UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
                UIGraphicsEndImageContext();
    
    
    
                NSString*imageID = [[NSUUIDUUID]UUIDString];
    
                NSString*homePath =NSTemporaryDirectory();
    
                homePath = [homePathstringByAppendingString:[NSStringstringWithFormat:@"/%@.jpg",imageID]];
    
                NSData*imageData =UIImageJPEGRepresentation(scaledImage,1.0);
    
                if([imageDatawriteToFile:homePathatomically:YES]) {
    
    //                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
    //                    dispatch_async(dispatch_get_main_queue(), ^{
    
                            [self.editorView insertImageUrl:[[NSURLfileURLWithPath:homePath]absoluteString]alt:imageID];//插入图片
    
    //                    });
    
    //                });
    
                }
    
            }
    
        }];
    
    //插入图片
    
    - (void)insertImageUrl:(NSString*)imageUrlalt:(NSString*)alt {
    
        NSString*trigger = [NSStringstringWithFormat:@"RE.insertImage(\"%@\", \"%@\");", imageUrl, alt];
    
        [self evaluateJavaScript:trigger completionHandler:nil];
    
    }
    
    
    
    RE.insertImage =function(url, alt) {
    
    varhtml = 'img H5代码';
    
    document.execCommand('insertHTML',false, html);
    
    }
    
    

    上边这段代码执行在了主线程,看注释掉的那一块

    猜测和浏览器异步加载图片有关,有明白的还请指教一二.

    相关文章

      网友评论

          本文标题:WKWebView 加载沙盒图片问题异常

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