問題描述
我有一個長的水平分頁滾動視圖,對于每個頁面,我都放了一個全屏 UIImage.
I have a long horizontal paged scrollview, for each page, I put a fullscreen UIImage.
由于價格昂貴且無需一次添加所有 UIImage,目前我延遲加載圖像(pageIndex-1 + pageIndex + pageIndex+1 的圖像,使用 NSOperationQueue 僅供參考),并刪除所有其他 UIImageViews.
As it is expensive and not necessary to add all UIImage at once, currently I lazy load the images (pageIndex-1 + pageIndex + pageIndex+1 's images, using NSOperationQueue FYI), and remove all other UIImageViews.
Instrument 報告的內存使用情況在正常使用情況下是可以接受的,但我發現如果我繼續滾動(連續在 30 頁之間切換),臟內存會不斷增加,尤其是內存標簽 70",我認為它是原始的來自 UIImage 的數據.一旦我停止觸摸應用程序 3~4 秒,所有內存標簽 70"都會自動釋放.
The memory usage reported from Instrument is acceptable with normal usage, but I discover if I keep scrolling (switching between 30 pages continously), the Dirty memory would keep increasing, especially "Memory Tag 70" which I believe it is the raw data from UIImage. Once I stop touching the app for 3~4 seconds, ALL "Memory Tag 70" would be released automagically.
我已將 NSLog 放在多個位置,并且我確信當 UIImageViews 超出顯示范圍"時,它會從其父視圖中刪除.
I've put NSLog in several places and I'm sure the UIImageViews are removed from its superview when it is out of the "display range".
滾動是否會阻止主循環釋放這些內存?我能做些什么來防止這種情況發生?謝謝!
Is scrolling preventing the main loop to release those memory? What could I do to prevent such condition? Thanks!
p.s Dirty Size 的大幅下降是我停止滾動的那一刻.
p.s The huge drop in Dirty Size is the moment when I stop scrolling.
推薦答案
(只是猜測,我距離 Xcode 還差得很遠,假設是非 ARC 環境)
(Just a guess, I'm far from an Xcode to test it, and assuming a non-ARC environment)
可以使用
UIImage *image = [otherImageView.image retain];
otherImageView.image = nil;
[image release]; // the image won't go to the main autorelease pool
[otherImageView removeFromSuperview]; // No one has references to otherImageView, so it goes to the autorelease pool
為了避免圖像進入自動釋放池.
To avoid the image going to the autorelease pool.
這篇關于UIImageView/UIImage“內存標簽70"滾動時的釋放時機的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!