iOS 加载中文链接的图片

iOS 是不能加载链接中带中文的图片,只要URL串中带有中文,通过

[NSURL URLWithStirng:urlStr] 

 返回的URL对象将为nil,导致图片不能加载

若APP中使用中文连接图片的地方相对较少,可以针对单独的代码块进行修改,但是如果不知道中文URL出现在何处 ,就只能针对底层进行修改

通过方法交换改变上述方法 [NSURL URLWithStirng:urlStr] 的实现,如下

+(void)load{
    Method originMethod = class_getClassMethod([self class], @selector(URLWithString:));
    Method changeMethod = class_getClassMethod([self class], @selector(pk_UTF8URLWithString:));
    method_exchangeImplementations(originMethod, changeMethod);
}

+ (NSURL *)pk_UTF8URLWithString:(NSString *)urlString{
    if ([self hasChinese:urlString] == YES) {
        //对URL中的中文进行编码
        NSString *url = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        NSURL * resultURL = [NSURL pk_UTF8URLWithString:url];
        return resultURL;
    }else{
        NSURL * resultURL = [NSURL pk_UTF8URLWithString:urlString];
        return resultURL;
    }
}

+ (BOOL)hasChinese:(NSString *)str {
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff){
            return YES;
        }
    }
    return NO;
}

上述方法中 多了一个判断是否中文的方法。此方法也很关键 ,缺少此方法,将对系统多有的URL都会进行处理,增加此方法,只对中文进行处理,很大程度上减小了代码的影响范围

其中判断中文的最关键一行  if( a > 0x4e00 && a < 0x9fff) 表示中文的Unicode值

网上有很多文章中提到的中文的起止为 0x4e00 (19968) -- 0x9fa5 (40869),但是本文中用到的却是 0x9fff (40959)

一下为从多出网页中查询到的蛛丝马迹

这里是几个主要非英文语系字符范围

2E80~33FFh:中日韩符号区。收容康熙字典部首、中日韩辅助部首、注音符号、日本假名、韩文音符,中日韩的符号、标点、带圈或带括符文数字、月份,以及日本的假名组合、单位、年号、月份、日期、时间等。

3400~4DFFh:中日韩认同表意文字扩充A区,总计收容6,582个中日韩汉字。

4E00~9FFFh:中日韩认同表意文字区,总计收容20,902个中日韩汉字。

A000~A4FFh:彝族文字区,收容中国南方彝族文字和字根。

AC00~D7FFh:韩文拼音组合字区,收容以韩文音符拼成的文字。

F900~FAFFh:中日韩兼容表意文字区,总计收容302个中日韩汉字。

FB00~FFFDh:文字表现形式区,收容组合拉丁文字、希伯来文、阿拉伯文、中日韩直式标点、小符号、半角符号、全角符号等。

比如需要匹配所有中日韩非符号字符,那么正则表达式应该是^[u3400-u9FFF]+$ 
理论上没错, 可是我到msn.co.ko随便复制了个韩文下来, 发现根本不对, 诡异 
再到msn.co.jp复制了个‘お‘, 也不得行..

然后把范围扩大到^[u2E80-u9FFF]+$, 这样倒是都通过了, 这个应该就是匹配中日韩文字的正则表达式了, 包括我們臺灣省還在盲目使用的繁體中文

u 应该代表为Unicode编码。即在Unicode编码中4E00-9FFF为中文字符编码区

而关于中文的正则表达式, 应该是^[u4E00-u9FFF]+$, 和论坛里常被人提起的^[u4E00-u9FA5]+$很接近

需要注意的是论坛里说的^[u4E00-u9FA5]+$这是专门用于匹配简体中文的正则表达式, 实际上繁体字也在里面, 我用测试器测试了下‘中華人民共和國‘, 也通过了, 当然, ^[u4E00-u9FFF]+$也是一样的结果

以下内容介绍了其他的Unicode字符的范围  摘录自 https://www.cnblogs.com/zhoug2020/p/3372260.html 

unicode码的分布情况,够清楚了吧!不仅汉字,什么都有了! 
  ******************************************************* 
  0000..007F;  Basic  Latin 
  0080..00FF;  Latin-1  Supplement 
  0100..017F;  Latin  Extended-A 
  0180..024F;  Latin  Extended-B 
  0250..02AF;  IPA  Extensions 
  02B0..02FF;  Spacing  Modifier  Letters 
  0300..036F;  Combining  Diacritical  Marks 
  0370..03FF;  Greek 
  0400..04FF;  Cyrillic 
  0530..058F;  Armenian 
  0590..05FF;  Hebrew 
  0600..06FF;  Arabic 
  0700..074F;  Syriac     
  0780..07BF;  Thaana 
  0900..097F;  Devanagari 
  0980..09FF;  Bengali 
  0A00..0A7F;  Gurmukhi 
  0A80..0AFF;  Gujarati 
  0B00..0B7F;  Oriya 
  0B80..0BFF;  Tamil 
  0C00..0C7F;  Telugu 
  0C80..0CFF;  Kannada 
  0D00..0D7F;  Malayalam 
  0D80..0DFF;  Sinhala 
  0E00..0E7F;  Thai 
  0E80..0EFF;  Lao 
  0F00..0FFF;  Tibetan 
  1000..109F;  Myanmar   
  10A0..10FF;  Georgian 
  1100..11FF;  Hangul  Jamo 
  1200..137F;  Ethiopic 
  13A0..13FF;  Cherokee 
  1400..167F;  Unified  Canadian  Aboriginal  Syllabics 
  1680..169F;  Ogham 
  16A0..16FF;  Runic 
  1780..17FF;  Khmer 
  1800..18AF;  Mongolian 
  1E00..1EFF;  Latin  Extended  Additional 
  1F00..1FFF;  Greek  Extended 
  2000..206F;  General  Punctuation 
  2070..209F;  Superscripts  and  Subscripts 
  20A0..20CF;  Currency  Symbols 
  20D0..20FF;  Combining  Marks  for  Symbols 
  2100..214F;  Letterlike  Symbols 
  2150..218F;  Number  Forms 
  2190..21FF;  Arrows 
  2200..22FF;  Mathematical  Operators 
  2300..23FF;  Miscellaneous  Technical 
  2400..243F;  Control  Pictures 
  2440..245F;  Optical  Character  Recognition 
  2460..24FF;  Enclosed  Alphanumerics 
  2500..257F;  Box  Drawing 
  2580..259F;  Block  Elements 
  25A0..25FF;  Geometric  Shapes 
  2600..26FF;  Miscellaneous  Symbols 
  2700..27BF;  Dingbats 
  2800..28FF;  Braille  Patterns 
  2E80..2EFF;  CJK  Radicals  Supplement 
  2F00..2FDF;  Kangxi  Radicals 
  2FF0..2FFF;  Ideographic  Description  Characters 
  3000..303F;  CJK  Symbols  and  Punctuation 
  3040..309F;  Hiragana 
  30A0..30FF;  Katakana 
  3100..312F;  Bopomofo 
  3130..318F;  Hangul  Compatibility  Jamo 
  3190..319F;  Kanbun 
  31A0..31BF;  Bopomofo  Extended 
  3200..32FF;  Enclosed  CJK  Letters  and  Months 
  3300..33FF;  CJK  Compatibility 
  3400..4DB5;  CJK  Unified  Ideographs  Extension  A 
  4E00..9FFF;  CJK  Unified  Ideographs 
  A000..A48F;  Yi  Syllables 
  A490..A4CF;  Yi  Radicals 
  AC00..D7A3;  Hangul  Syllables 
  D800..DB7F;  High  Surrogates 
  DB80..DBFF;  High  Private  Use  Surrogates 
  DC00..DFFF;  Low  Surrogates 
  E000..F8FF;  Private  Use 
  F900..FAFF;  CJK  Compatibility  Ideographs 
  FB00..FB4F;  Alphabetic  Presentation  Forms 
  FB50..FDFF;  Arabic  Presentation  Forms-A 
  FE20..FE2F;  Combining  Half  Marks 
  FE30..FE4F;  CJK  Compatibility  Forms 
  FE50..FE6F;  Small  Form  Variants 
  FE70..FEFE;  Arabic  Presentation  Forms-B 
  FEFF..FEFF;  Specials 
  FF00..FFEF;  Halfwidth  and  Fullwidth  Forms 
  FFF0..FFFD;  Specials 
  10300..1032F;  Old  Italic 
  10330..1034F;  Gothic 
  10400..1044F;  Deseret 
  1D000..1D0FF;  Byzantine  Musical  Symbols 
  1D100..1D1FF;  Musical  Symbols 
  1D400..1D7FF;  Mathematical  Alphanumeric  Symbols 
  20000..2A6D6;  CJK  Unified  Ideographs  Extension  B 
  2F800..2FA1F;  CJK  Compatibility  Ideographs  Supplement 
  E0000..E007F;  Tags 
  F0000..FFFFD;  Private  Use 
  100000..10FFFD;  Private  Use 

http://blog.oasisfeng.com/2006/10/19/full-cjk-unicode-range/

因为FontRouter新版本开发的需要,在网上搜索了一下汉字的Unicode范围,普遍给出了“U+4E00..U+9FA5”。但事实上这个范围是不完整的,甚至连基本的全角(中文)标点也未包含在内。为此,我特地查询了Unicode官方的Code Charts数据库,并根据最新的Unicode 5.0版整理如下:

注:在绝大多数应用场合中,我们可以仅用(1)、(2)、(3)、(4)、(5)的集合作为CJK判断的依据。

1)标准CJK文字
http://www.unicode.org/Public/UNIDATA/Unihan.html

Code point range
Block name
Release

U+3400..U+4DB5
CJK Unified Ideographs Extension A
3.0

U+4E00..U+9FA5
CJK Unified Ideographs
1.1

U+9FA6..U+9FBB
CJK Unified Ideographs
4.1

U+F900..U+FA2D
CJK Compatibility Ideographs
1.1

U+FA30..U+FA6A
CJK Compatibility Ideographs
3.2

U+FA70..U+FAD9
CJK Compatibility Ideographs
4.1

U+20000..U+2A6D6
CJK Unified Ideographs Extension B
3.1

U+2F800..U+2FA1D
CJK Compatibility Supplement
3.1

2)全角ASCII、全角中英文标点、半宽片假名、半宽平假名、半宽韩文字母:FF00-FFEF
http://www.unicode.org/charts/PDF/UFF00.pdf

3)CJK部首补充:2E80-2EFF
http://www.unicode.org/charts/PDF/U2E80.pdf

4)CJK标点符号:3000-303F
http://www.unicode.org/charts/PDF/U3000.pdf

5)CJK笔划:31C0-31EF
http://www.unicode.org/charts/PDF/U31C0.pdf

6)康熙部首:2F00-2FDF
http://www.unicode.org/charts/PDF/U2F00.pdf

7)汉字结构描述字符:2FF0-2FFF
http://www.unicode.org/charts/PDF/U2FF0.pdf

8)注音符号:3100-312F
http://www.unicode.org/charts/PDF/U3100.pdf

9)注音符号(闽南语、客家语扩展):31A0-31BF
http://www.unicode.org/charts/PDF/U31A0.pdf

10)日文平假名:3040-309F
http://www.unicode.org/charts/PDF/U3040.pdf

11)日文片假名:30A0-30FF
http://www.unicode.org/charts/PDF/U30A0.pdf

12)日文片假名拼音扩展:31F0-31FF
http://www.unicode.org/charts/PDF/U31F0.pdf

13)韩文拼音:AC00-D7AF
http://www.unicode.org/charts/PDF/UAC00.pdf

14)韩文字母:1100-11FF
http://www.unicode.org/charts/PDF/U1100.pdf

15)韩文兼容字母:3130-318F
http://www.unicode.org/charts/PDF/U3130.pdf

16)太玄经符号:1D300-1D35F
http://www.unicode.org/charts/PDF/U1D300.pdf

17)易经六十四卦象:4DC0-4DFF
http://www.unicode.org/charts/PDF/U4DC0.pdf

18)彝文音节:A000-A48F
http://www.unicode.org/charts/PDF/UA000.pdf

19)彝文部首:A490-A4CF
http://www.unicode.org/charts/PDF/UA490.pdf

20)盲文符号:2800-28FF
http://www.unicode.org/charts/PDF/U2800.pdf

21)CJK字母及月份:3200-32FF
http://www.unicode.org/charts/PDF/U3200.pdf

22)CJK特殊符号(日期合并):3300-33FF
http://www.unicode.org/charts/PDF/U3300.pdf

23)装饰符号(非CJK专用):2700-27BF
http://www.unicode.org/charts/PDF/U2700.pdf

24)杂项符号(非CJK专用):2600-26FF
http://www.unicode.org/charts/PDF/U2600.pdf

25)中文竖排标点:FE10-FE1F
http://www.unicode.org/charts/PDF/UFE10.pdf

26)CJK兼容符号(竖排变体、下划线、顿号):FE30-FE4F
http://www.unicode.org/charts/PDF/UFE30.pdf

以上翻译自Unicode官方网站,部分译法可能不够准确,还望大家予以指正!如有疏漏、错误之处也请一并指出,多谢!

原文地址:https://www.cnblogs.com/tianlin106/p/14121586.html