Wednesday, June 20, 2012

How to Take ScreenShot and save in Photo Album

-(void)saveImageInPhotoAlbum{
    UIGraphicsBeginImageContext(
                              CGSizeMake(self.view.frame.size.width,
                                       self.view.frame.size.height));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData *imageData = [NSData dataWithData:
                                UIImagePNGRepresentation(screenShot)];
    UIImage *image=[UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
          selector:@selector(saveImageInPhotoAlbum) 
    userInfo:nil repeats:NO];


// include QuartzCore Framework 
// #import <QuartzCore/QuartzCore.h>

Tuesday, June 19, 2012

Get Seconds from TimeFormat

code :

-(NSString *)getSeconds:(NSString *)timeString {
    NSArray *time    = [timeString componentsSeparatedByString: @":"];
    NSString *hours = [time objectAtIndex: 0];
    NSString *mins  = [time objectAtIndex: 1];
    NSString *secs  = [time objectAtIndex: 2];
    int total              = (([hours intValue] * 3600)+ ([mins intValue] * 60) + [secs intValue]);
    NSString* totalSeconds = [NSString stringWithFormat:@"%d", total];
    return totalSeconds;
}

usage: 


NSLog(@"Seconds : %@", [self getSeconds: @"01:30:48"]);

Friday, June 08, 2012

NSUserDefaults

Save :

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"stringKey"];


// saving an NSInteger
[prefs setInteger:100 forKey:@"integerKey"];


// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];



Retrieve :

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


// getting an NSString
NSString *myString = [prefs stringForKey:@"
stringKey"];


// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"
integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"
floatKey"];

Reading data from .plist file

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *filePath = [bundlePath stringByAppendingPathComponent:@"Info.plist"];
    NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:filePath] retain];
   
    versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,100,60,25)];
    versionLabel.backgroundColor = [UIColor clearColor];
    versionLabel.textColor = [UIColor whiteColor];
    versionLabel.font = [UIFont systemFontOfSize:10];
    NSString *versionString = [NSString stringWithFormat:@"v%@", [plistData objectForKey:@"CFBundleVersion"]];
    versionLabel.text = versionString;
    [self.view addSubview:versionLabel];

or simply :

NSString *s = [NSString stringWithFormat:@"App Version %@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
    versionLabel.text = s;

Wednesday, June 06, 2012

Prevent The iPhone From Sleeping

The code below will prevent the iPhone from dimming its screen and ultimately going to sleep.

[UIApplication sharedApplication].idleTimerDisabled = YES;

Monday, June 04, 2012

String matching and comparison

NSString* str1 = @"hello";
NSString* str2 = @"hello world";
NSRange matching = [str2 rangeOfString : str1];

// Searching for a string within another string
if(matching.location == NSNotFound){
    // str1 not found in str2
} else {
    // str1 found in str2
}

// Checking for equality
if([str1 isEqualToString:@"hello"]){
    // str1 is equal to "hello"
} else {
    // str1 is not equal to "hello"
}

Play a video in full screen

1. Include MediaPlayer framework in the project
2. Import header file in the view controller
3. Declare the player instance in the controller header file:
MPMoviePlayerViewController *playerViewController;

Sample Code : 
----------------
- (void) playVideo:(NSString *)fileName
{
    NSString *url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
    playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:[playerViewController moviePlayer]];
    [self.view addSubview:playerViewController.view];
   
    //play movie
    MPMoviePlayerController *player = [playerViewController moviePlayer];
    [player play];        
}

// The call back
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:player];
    player.initialPlaybackTime = -1;
    [player stop];
    [player.view removeFromSuperview];   
    [player release];   
}

Scaling an image in iOS

The UIImage class can be used to represent an image. The following snippet scales an image according to a size which is provided with a CGSize parameter.

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators: Closed Range operators  Half-Open Range Operators One-Sided Ranges Closed Range Operators:  a...b It defines...