開發Mac平台的應用程序時遇到一個熱更新圖標的需求。
Mac的應用程序是.app
bundle,圖標文件放在test.app/Contents/Resources/
路徑,在test.app/Contents/Info.plist
中指定。
可是,更換.icns
圖標文件后,Finder里不即時更新顯示。如何繞開緩存機制,強制刷新顯示圖標呢?當然,得是以編程方式實現。
搜到一些建議。
重建Launch Services數據庫,
lsregister -kill -seed -r lsregister -f <.app path>
無效。
修改bundle的時間戳。
touch /Applications/App.app
無效。
有人提到“創建-刪除”一個文件的方法,有人嘗試了這個方法,告訴大家說,他嘗試了所有bundle里的目錄都無效,除了test.app/
。測試有效。謝謝他。
Qt測試代碼,創建一個文件夾,再刪除,
QDir junk; junk.mkdir(strBundlePath + "/junk"); junk.rmdir(strBundlePath + "/junk");
可是緊接着在其他電腦上測試發現,有時還是無效。
研究發現,無效時,test.app/
bundle目錄里有一個文件Icon?
。
它其實是Icon\r
,終端里的自動補全會顯示為Icon^M
。這個文件是干嗎用的?怎么產生的?這里有詳細介紹。
說是改變文件夾圖標時會產生這個文件,實測發現,只在右鍵test.app
bundle選擇“Get Info”,拖動圖片文件到Info對話框左上角圖標位置時,會產生這個文件。不知道有沒有其他動作也會。
有這個文件的時候,前面說的“創建-刪除”文件夾/文件方法無效,可能的原因是“when you change the icon, it is not actually applied to the folder itself but rather to the 'Icon\r' file inside the folder”。
解決方案是刪除它。測試代碼如下,
QFile jfile(strBundlePath + "/Icon\r"); if(jfile.exists()) jfile.remove(); QDir junk; junk.mkdir(strBundlePath + "/junk"); junk.rmdir(strBundlePath + "/junk");
項目里加上這樣的代碼真是無奈,所謂笨辦法吧。
Windows平台上,新圖標編譯在.exe
文件里,可是.exe
文件熱更新后,桌面的快捷方式圖標一樣存在緩存不能立刻刷新的問題。思路類似,沒再嘗試,舉例如下,來自這篇博客。
rem 關閉Windows外殼程序explorer taskkill /f /im explorer.exe rem 清理系統圖標緩存數據庫 attrib -h -s -r "%userprofile%\AppData\Local\IconCache.db" del /f "%userprofile%\AppData\Local\IconCache.db" attrib /s /d -h -s -r "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\*" del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_32.db" del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_96.db" del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_102.db" del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_256.db" del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_1024.db" del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_idx.db" del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_sr.db" rem 清理 系統托盤記憶的圖標 echo y|reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v IconStreams echo y|reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v PastIconsStream rem 重啟Windows外殼程序explorer start explorer