Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One thing you will need to address when saving images returned by <code>UIImagePickerVIewController</code> is that writing the data to disk will almost always be unacceptably slow. Your UI will hang while the writing is occurring. So, you should <em>always</em> execute these types of operations in an asynchronous queue. Even if the performance seems good enough for your application when testing, <em>you should still do it an asynch queue</em> -- you never know what other processes the device might have going on which might slow the save down once your app is in the hands of users.</p> <p>Newer versions of iOS make saving photos asynchronously really, really easy using Grand Central Dispatch (GCD). The steps are:</p> <ol> <li>Create an <code>NSBlockOperation</code> which saves the image</li> <li>In the block operation's completion block, read the image from disk &amp; display it (the only caveat here is that you must use the main queue to display the image: <em>all UI operations must occur on the main thread</em>).</li> <li>Add the block operation to an operation queue and watch it go!</li> </ol> <p>That's it. And here's the code:</p> <pre><code>// Grab the image UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; // Create a block operation with our saves NSBlockOperation* saveOp = [NSBlockOperation blockOperationWithBlock: ^{ [UIImagePNGRepresentation(image) writeToFile:file atomically:YES]; }]; // Use the completion block to update our UI from the main queue [saveOp setCompletionBlock:^{ [[NSOperationQueue mainQueue] addOperationWithBlock:^{ UIImage *image = [UIImage imageWithContentsOfFile:file]; // TODO: Assign image to imageview }]; }]; // Kick off the operation, sit back, and relax. Go answer some stackoverflow // questions or something. NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:saveOp]; </code></pre> <p>Once you are comfortable with this code pattern, you will find yourself using it a lot. It's incredibly useful when generating large datasets, long operations on load, etc. Essentially, any operation that makes your UI laggy in the least is a good candidate for this code. Just remember, you can't do anything to the UI while you aren't in the main queue and everything else is cake.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload