Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
959 views
in Technique[技术] by (71.8m points)

weex iOS <WXImgLoaderProtocol> 无法加载带中文的图片网址

  • (id<WXImageOperationProtocol>)downloadImageWithURL:(NSString )url imageFrame:(CGRect)imageFrame userInfo:(NSDictionary )options completed:(void(^)(UIImage image, NSError error, BOOL finished))completedBlock {

    // 如果页面传过来的图片网址带中文,url参数打印出来为null,安卓加载没问题
     
     NSLog(@"----%@", url);
     // 打印结果:----(null)

    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

weex框架源码中有个类WXURLRewriteDefaultImpl,该类中有个方法对URLString字符串的处理欠妥当,方法和更改方式见如下注释:

- (NSURL *)rewriteURL:(NSString *)url
     withResourceType:(WXResourceType)resourceType
         withInstance:(WXSDKInstance *)instance
{
   // url 需要先进行一次 NSUTF8 转码,否则,如果url里面有中文,得到的completeURL为nil,导致带中文的图片网址加载不出来
    NSString *UTF8URLString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *completeURL = [NSURL URLWithString:UTF8URLString];
    if ([completeURL isFileURL]) {
        return completeURL;
    } else if ([self isLocalURL:completeURL]) {
        NSString *resourceName = [[completeURL host] stringByAppendingString:[completeURL path]];
        NSURL *resourceURL = [[NSBundle mainBundle] URLForResource:resourceName withExtension:@""];
        if (!resourceURL) {
            WXLogError(@"Invalid local resource URL:%@, no resouce found.", url);
        }
        
        return resourceURL;
    } else {
        // 这里改为传转码后的url
        return [instance completeURL:UTF8URLString];
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...