Tag Archive for 'development'

Getting UIImage to work with NSCoding encodeWithCoder

Out of the box, UIImage doesn’t support the NSCoding protocol which means it won’t automatically serialize if you stuff these in a dictionary or object and archive (using NSKeyedArchiver for example). The easy solution is to convert this into an NSData object which does support the protocol:

//convert from PNG to NSData, other method available for JPG
NSData *imageData = UIImagePNGRepresentation(imageToArchive);
[self.currentItem setValue:imageData forKey:@"image"];
 
//after the archived object is unarchived, you can retrieve the image like this
NSData *imageData = [currentItem valueForKey:@"image"];
if(imageData){
       UIImage *image = [UIImage imageWithData:imageData];
}

Without doing this, and trying to archive a UIImage will just result in:


-[UIImage encodeWithCoder:]: unrecognized selector sent to instance

Pretty simple. The key is the helper method UIImagePNGRepresentation found in the UIKit Function Reference.

More Than Just Guidelines

The iPhone Human Interface Guidelines are more than just that — it is essential for you to understand them in order to ensure that your app will be approved for sale in the App Store.

From Apple:
“Applications must adhere to the iPhone Human Interface Guidelines as outlined in iPhone SDK Agreement section 3.3.5.”

Consider this important guideline in the Table Views, Text Views, and Web Views section of the iHIG:

“Table views provide feedback when users select list items. Specifically, when an item can be selected, the row containing the item highlights briefly when a user selects it to show that the selection has been received. Then, an immediate action occurs: Either a new view is revealed or the row displays a checkmark to indicate that the item has been selected. The row never remains highlighted, because table views do not display persistent selected state.”



The easy part to miss when implementing your own View Controller is “The row never remains highlighted”.

In your own custom UIViewController subclasses, you’ll want to address this within the

- (void)viewWillAppear:(BOOL)animated UIViewController delegate method.

Given a UITableView member property, theTableView, you can make sure the selected row gets deselected after popping the previous View Controller:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
 
//deselect any selected rows
NSIndexPath *selectedRowPath = [theTableView indexPathForSelectedRow];
[theTableView deselectRowAtIndexPath:selectedRowPath animated:YES];
...
}



Treat the guidelines as the law of the land, and you and your users will attain App Store nirvana.