Depending on the resolution of the image, UIImagePNGRepresentation
can be quite slow, like any write to the file system.
You should always perform these types of operations in an asynchronous queue. Even if the performance seems good enough for your application when testing, you should still make it an asynchronous queue - you never know what other processes can occur on the device, which can slow down saving after your application is in the hands of users.
Newer versions of iOS make it possible to save asynchronously, very simply, using Grand Central Dispatch (GCD). Steps:
- Create an
NSBlockOperation
that Saves the Image - In the block for completing the operation of the block, read the image from the disk and display it. The only caveat is that you should use the main queue to display the image: all user interface operations must be performed in the main thread.
- Add a block operation to the operation queue and see how it happens!
What is it. And here is the code:
Once you like this code template, you can use it. This is incredibly useful when creating large data sets, lengthy load operations, etc. In fact, any operation that makes your user interface concise is at least a good candidate for this code. Just remember, you canβt do anything with the interface until youβre in the main line and the rest is cake.
source share