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官方網站,部分譯法可能不夠准確,還望大家予以指正!如有疏漏、錯誤之處也請一並指出,多謝!