Home
Cached Images UITableViewCell using Grand Central Dispatch PDF Print E-mail
Programming
Written by Nate Lyman   
Monday, 09 January 2012 10:05

Recently I was working on an iPhone App that was struggling with a really slow UITableView.  The reason it was slow, was because it was loading remote images for every UITableViewCell on each load.  The best approach would be to load the image once, cache it, and show the cached file if it exists.

Over my years of doing iOS development I have a pretty decent collection of utilities and classes to do many common tasks.  Loading remote images in a UITableViewCell is one of them.  I put a demo project up on GitHub.  You can download it at: https://github.com/natelyman/NLCachedImageTableViewCell

I'll do a brief walk through if you want to learn what I'm doing here...

 

NLCachedImageTableViewCell.h:

In the header file I define a couple properties that the caller will use in this UITableViewCell.  The first is an NSString called imageUrlString, which the caller will pass in to load the remote image. The second is a UIImage called placeholderImage. The placeholder image is an optional image that the UITableView will display while the remote images are loading.  This is pretty important if the images are large.  If they're thumbnails it is less important.

NLCachedImageTableViewCell.m:

The setImageUrlString: method is where all the magic happens.  The first step is to create a unique filename. I use SHA1 to generate the filename because I want to avoid collisions.  I have used this class in some very large apps that do extensive image caching, so collisions can happen if you go with a simple naming pattern.  We store the file in the Applications' Documents directory.  You could store it in the Temporary or Cache directory, but iOS 5 is a lot more pro-active about clearing these.  If you are loading in data that changes often, say a stream from Twitter, I'd recommend using the Cache directory.  If you need the cached images to stick around for longer than a day or two, leave it in the Documents directory.

Next we see if the file exists, if it does we simply load the stored file and set the UIImageView's UIImage property and we're good to go.  If the file doesn't exist we need to load it.  To do this we use Grand Central Dispatch to asynchronously load the images.  There are other ways to do this, but in the UITableView use case, I have found Grand Central Dispatch to be the most performant.  I make the network call to load the image, save the image to the file system using the filename we came up with earlier.  I then pass the UIImage into the cell's UIImageView.

As a bonus, if you checkout the demo project, I used Twitter as a data source using iOS 5's new Twitter framework.  I then load the profile images using the above Table View Cells.

Last Updated on Monday, 09 January 2012 10:29
 
Fixing git after installing OS X Lion PDF Print E-mail
Programming
Written by Nate Lyman   
Friday, 22 July 2011 03:08

So, I just installed Lion yesterday and went to bed.  Today I got up and was ready to do a pull and dive into work but git was no longer available on the command line.  So here is what I did to fix it.

Enable .bashrc in Terminal

Open terminal

sudo vi /etc/profile

enter password

add the following line at the end of the file: [ -r $HOME/.bashrc ] && source $HOME/.bashrc

Save and Exit vi

Add git to PATH

Next, enter: vi ~/.bashrc

Add: PATH=/usr/local/git/bin:$PATH

Save and exit vi

Quit Terminal

Launch Terminal

You should be good to go.

 

 

Last Updated on Friday, 22 July 2011 03:28
 
RedLaser Live PDF Print E-mail
Technology
Written by Nate Lyman   
Thursday, 02 June 2011 17:13

For the last month or so I've been working on an app at work called RedLaser Live.  Checkout the press release:

RedLaser Live Press Release

SAN JOSE, Calif.--(EON: Enhanced Online News)--RedLaser, a leading barcode and QR code scanning mobile app, is bringing a brand new mobile experience to Sasquatch! Music Festival this weekend by launching a new sister app, RedLaser LIVE. The company is also offering Sasquatch! attendees the chance to win free merchandise and entry into exclusive acoustic performances – all through QR code contests. The new app, RedLaser LIVE, is a first-of-its kind geo-location tool, designed to help festival-goers keep tabs on friends, campsites, cars and more. The app, available for free on iPhone®, will also share the locations of special promotions throughout the festival, such as discounts at food stands and t-shirt giveaways at the RedLaser booth onsite.
 
Installing GIT on Linux PDF Print E-mail
Written by Nate Lyman   
Monday, 28 March 2011 09:10

Just setup GIT on one of my servers to fully handle my SCM needs.  I used to use Beanstalk, but I don't need another bill when I am already paying for servers.

I found a great tutorial and thought I'd share it, just in case you find yourself in a similar situation.

Hosting GIT repositories the Easy and Secure way

 

Last Updated on Monday, 28 March 2011 09:13
 
Custom image for UIBarButtonItem in UINavigationController PDF Print E-mail
Programming
Written by Nate Lyman   
Monday, 07 March 2011 05:29

For a recent project I had to use a custom image for a UIBarButtonItem in a UINavigationController.  To clarify, this would be an image button, not a standard bordered button with an Icon.

Checkout the code below:

 

Show/Hidden php code

 

 

Last Updated on Tuesday, 08 March 2011 07:52
 
« StartPrev12345678910NextEnd »

Page 1 of 14

@natelyman

natelyman: RT @mtabini: OH: “Every time I see one of those people dressed like an iPhone at an Apple launch, I want to kick their home button.”
natelyman: Recruiter wants a backend developer with 8 years of HTML5 experience. Good luck sir.
natelyman: Uhhh... No. Nice try LinkedIn. #AspiringForCSharpNOT http://t.co/mgNDavXI