NSCoding & NSUserDefaults to store something simple…

Lets say you created an app which lets you select a person from the Address Book and you wish to set that person as the default user to use each time you launch the app. You want to store that preference in NSUserDefaults. If there is a lot of data to that person, you probably don’t want to store each key, key by key…This is where you can modify your NSObject class to conform to NSCoding and quickly save & load your data. 1. In you NSObject Class add theprotocol. @interface CustomObject : NSObject <NSCoding> 2. Now add these 2 methods to your NSObject Class: – (void) encodeWithCoder:(NSCoder*)encoder; – (id)… Read More

Continue Reading

How to mask a UIImage in iOS

– (void)viewDidLoad { [super viewDidLoad]; UIImage *imageToMask = [UIImage imageNamed:@”koko.jpg”]; UIImageView *imageToMaskImgView = [[UIImageView alloc] initWithImage:imageToMask]; CGRect imgRect = CGRectMake(0, 0, imageToMaskImgView.frame.size.width, imageToMaskImgView.frame.size.height); UIView *maskMaster = [[UIView alloc] initWithFrame:imgRect]; [maskMaster setBackgroundColor:[UIColor whiteColor]]; [maskMaster addSubview:imageToMaskImgView]; UIGraphicsBeginImageContext(maskMaster.bounds.size); [maskMaster.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSLog(@”%@ is the view image”, viewImage); UIImage *bFooImg = [UIImage imageNamed:@”blackapple.png”]; self.myPic.image = [self maskImage:bFooImg withMask:viewImage]; } – (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage { CGImageRef maskRef = maskImage.CGImage; CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), NULL, false); CGImageRef masked = CGImageCreateWithMask([image CGImage], mask); //memman CGImageRelease(mask); CGImageRelease(maskRef); return [UIImage imageWithCGImage:masked]; }

Continue Reading

Random yet helpful tips for Xcode 4.3.1

I’ve been learning a lot about tools for developing iOS apps and its amazing how much these tools, when you learn how to use them, can help you: Source Control Snippets Edit in Scope Speech Assistant Appirater TestFlight SDK Let’s look at these one by one: Source Control. This tool is for creating “Time Machine” like versions of your project as you progress through your project and make changes to it. The idea is two-fold: (a) Keep important subversions, i.e. version 1.0, 1.0.1, 1.0.5, 1.1.0 etc of your project as you make changes to it. And (b), share those versions with a team of developers so that you all have… Read More

Continue Reading

AddressBook UI

Have you ever tried to get data fields from the address book and ran into the wonderful world of ABPerson reference only to find it reads like the blueprints to a space rocket? Here is the straight out code on how to do it: //Commented out code fails because the values are NOT STRINGS, but rather NSCFType) //NSString *fone = (__bridge NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty); //self.telLabel.text = fone; //This on the other hand, does work ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty); CFStringRef fone = ABMultiValueCopyValueAtIndex(phoneMulti, 0); NSString *foneObject = (__bridge NSString*)fone; NSLog(@”phone is %@”,foneObject); self.telLabel.text = foneObject; CFRelease(fone); //Haven’t tried with URL //NSString *url = (__bridge NSString*)ABRecordCopyValue(person, kABPersonURLProperty); //self.urlLabel.text = [NSString stringWithFormat:url]; //SocialProfiles… Read More

Continue Reading