More on Low-Memory Warnings

You have seen how view controllers handle low-memory warnings – they are sent the message didReceiveMemoryWarning and release their view if it is not on the screen. This is an appropriate solution to handling a low-memory warning: an object gets rid of anything it isn’t currently using and can recreate later. Objects other than view controllers may have data that they aren’t using and can recreate later. The ImageStore is such an object – when its images aren’t on the screen, it is okay to destroy them because they can be loaded from the filesystem when they’re needed again.

Whenever a low-memory warning occurs, UIApplicationDidReceiveMemoryWarningNotification is posted to the notification center. Objects that want to implement their own low-memory warning handlers can register for this notification. In ImageStore.m, edit the init method to register the image store as an observer of this notification.

-​ ​(​i​d​)​i​n​i​t​
{​
 ​ ​ ​ ​i​f​ ​(​d​e​f​a​u​l​t​I​m​a​g​e​S​t​o​r​e​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​ ​d​e​f​a​u​l​t​I​m​a​g​e​S​t​o​r​e​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​s​e​l​f​ ​=​ ​[​s​u​p​e​r​ ​i​n​i​t​]​;​
 ​ ​ ​ ​i​f​ ​(​s​e​l​f​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​d​i​c​t​i​o​n​a​r​y​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​N​S​N​o​t​i​f​i​c​a​t​i​o​n​C​e​n​t​e​r​ ​*​n​c​ ​=​ ​[​N​S​N​o​t​i​f​i​c​a​t​i​o​n​C​e​n​t​e​r​ ​d​e​f​a​u​l​t​C​e​n​t​e​r​]​;​
 ​ ​ ​ ​[​n​c​ ​a​d​d​O​b​s​e​r​v​e​r​:​s​e​l​f​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​e​l​e​c​t​o​r​:​@​s​e​l​e​c​t​o​r​(​c​l​e​a​r​C​a​c​h​e​:​)​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​n​a​m​e​:​U​I​A​p​p​l​i​c​a​t​i​o​n​D​i​d​R​e​c​e​i​v​e​M​e​m​o​r​y​W​a​r​n​i​n​g​N​o​t​i​f​i​c​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​b​j​e​c​t​:​n​i​l​]​;​

 ​ ​ ​ ​}​

 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

Now, a low-memory warning will send the message clearCache: to the ImageStore instance. In ImageStore.m, implement clearCache: to remove all the UIImage objects from the ImageStore’s dictionary.

-​ ​(​v​o​i​d​)​c​l​e​a​r​C​a​c​h​e​:​(​N​S​N​o​t​i​f​i​c​a​t​i​o​n​ ​*​)​n​o​t​e​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​f​l​u​s​h​i​n​g​ ​%​d​ ​i​m​a​g​e​s​ ​o​u​t​ ​o​f​ ​t​h​e​ ​c​a​c​h​e​"​,​ ​[​d​i​c​t​i​o​n​a​r​y​ ​c​o​u​n​t​]​)​;​
 ​ ​ ​ ​[​d​i​c​t​i​o​n​a​r​y​ ​r​e​m​o​v​e​A​l​l​O​b​j​e​c​t​s​]​;​
}​

Removing an object from a dictionary releases it, so flushing the cache releases all of the images. Images that aren’t being used by other objects are deallocated, and when they are needed again, they will be reloaded from the filesystem. If an image is currently displayed in the ItemDetailViewController’s imageView, then it will not be deallocated. An image that is being used during a memory warning remains in memory until the object using it releases it. At that point, it is deallocated and will be reloaded from the filesystem when it is needed again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.9.197