Iphone Developer Technological

Using NSUserDefaults

This is handy when saving credentials to a device so the user doesn’t have to re-login every time he launches the app. NOTE: This is not useful for saving large amounts of data or data that will change frequently. Use iCloud or persistent state stores such as databases or xml or plist files for that!

Once you have the data you wish to save somewhere in your app, call the following:

// call the store object
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// set an object for it
[prefs setObject:imageView.image forKey:@”keyToLookupImage”];
// call sync to save it
[prefs synchronize];

then later in the beginning or launch of an app you can get that data like so:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = nil;
//If myString = nil, then do all this, otherwise do nothing…
UIImage *retrievedImage = [prefs stringForKey:@”keyToLookupImage”];
imageView.image = retrievedImage;

You have just saved your user a good deal of grief and he’ll thank you for it!

Iphone Developer Technological

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) initWithCoder:(NSCoder*)decoder;

3. Now implement the methods

- (void) encodeWithCoder:(NSCoder*)encoder {
    // If parent class also adopts NSCoding, include a call to
    // [super encodeWithCoder:encoder] as the first statement.

    [encoder encodeObject:someArray forKey:@"someArray"];
    [encoder encodeInteger:someInteger forKey:@"someInteger"];
    [encoder encodeBool:someBool forKey:@"someBool"];

- (id) initWithCoder:(NSCoder*)decoder {
    if (self = [super init]) {
      // If parent class also adopts NSCoding, replace [super init]
      // with [super initWithCoder:decoder] to properly initialize.

      // NOTE: Decoded objects are auto-released and must be retained
      someArray = [[decoder decodeObjectForKey:@"someArray"] retain];
      someInteger = [decoder decodeIntegerForKey:@"someInteger"];
      someBool = [decoder decodeBoolForKey:@"someBool"];
    return self;

4. Finally, archive your object doing this:

CustomObject *object; // Assume this exists
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
// Now do something with your NSData object, such
// as write it to a file or save it to NSUserDefaults

5. And retrieve it using this code:

NSData *encodedObject;
// First retrieve your NSData object by reading it
// from a file or grabbing it from NSUserDefaults
CustomObject *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];

6. Now simply use your object where and as needed!


Iphone Developer Technological

How to mask a UIImage in iOS

Mask UIImage iOS by Marcio Valenzuela
Mask UIImage iOS by Marcio Valenzuela

– (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];
[maskMaster.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
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),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];

Translate »