Translate

Thursday, 20 September 2012

Creating the text as image..

Text as image
task : how to create the image from text in objective-c...

the call  :
sourceImage.image=[UIImage addText:sourceImage.image text:@"Spynet"];
    modifiedImage.image=[UIImage imageFromText:@"Spynet....."];

.h file:

#import <UIKit/UIKit.h>

@interface UIImage (MyImage)


+ (UIImage*)imageFromView:(UIView*)view;
+ (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize;
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
+ (void)beginImageContextWithSize:(CGSize)size;
+ (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds;

//masking the image ....

+ (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage;


//image from text...
+ (UIImage *)addText:(UIImage *)img text:(NSString *)text1;
+(UIImage *)imageFromText:(NSString *)text;
@end

 .m file :

#import "UIImage+MyImage.h"

@implementation UIImage (MyImage)

+ (void)beginImageContextWithSize:(CGSize)size
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        if ([[UIScreen mainScreen] scale] == 2.0) {
            UIGraphicsBeginImageContextWithOptions(size, YES, 2.0);
        } else {
            UIGraphicsBeginImageContext(size);
        }
    } else {
        UIGraphicsBeginImageContext(size);
    }
}

+ (void)endImageContext
{
    UIGraphicsEndImageContext();
}

+ (UIImage*)imageFromView:(UIView*)view
{
    [self beginImageContextWithSize:[view bounds].size];
    BOOL hidden = [view isHidden];
    [view setHidden:NO];
    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    [self endImageContext];
    [view setHidden:hidden];
    return image;
}

+ (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize
{
    UIImage *image = [self imageFromView:view];
    if ([view bounds].size.width != newSize.width ||
        [view bounds].size.height != newSize.height) {
        image = [self imageWithImage:image scaledToSize:newSize];
    }
    return image;
}

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    [self beginImageContextWithSize:newSize];
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    [self endImageContext];
    return newImage;
}


+ (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect(myImage.CGImage, bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGSize asd =  croppedImage.size;
    return croppedImage;
}




+ (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);
   
    CGImageRelease(mask);
    CGImageRelease(maskRef);
    return [UIImage imageWithCGImage:masked];
   
}


+ (UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
    int w = img.size.width;
    int h = img.size.height;
    //lon = h - lon;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
   
    char* text  = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);
   
   
    //rotate text
    CGContextShowTextAtPoint(context, 0, 5, text, strlen(text));
   
   
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
   
    return [UIImage imageWithCGImage:imageMasked];
}


+(UIImage *)imageFromText:(NSString *)text
{
    CGSize maximumSize = CGSizeMake(300, 1000); //set width for string to wrap.
    UIFont *font = [UIFont boldSystemFontOfSize:16];
    CGSize strSize = [text sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions(strSize,NO,0.0);
    else
        // iOS is < 4.0
        UIGraphicsBeginImageContext(strSize);
   
    CGRect newframe = CGRectMake(0, 0, strSize.width, strSize.height);
    [text  drawInRect:newframe
             withFont:font
        lineBreakMode:UILineBreakModeWordWrap
            alignment:UITextAlignmentLeft];
    UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();   
    return testImg;
}


@end


use this file  and cloud link will be...




how to make Blur effect ?



Task : image blur in ios by this category file....


Image coding : 

.h file:

#import <Foundation/Foundation.h>


@interface UIImage (StackBlur)
    - (UIImage*) stackBlur:(NSUInteger)radius;
    - (UIImage *) normalize ;

@end

.m file:
#import "UIImage+StackBlur.h"


@implementation  UIImage (StackBlur)


// Stackblur algorithm
// from
// http://incubator.quasimondo.com/processing/fast_blur_deluxe.php
// by  Mario Klingemann

- (UIImage*) stackBlur:(NSUInteger)inradius
{
    int radius=inradius; // Transform unsigned into signed for further operations
   
    if (radius<1){
        return self;
    }
    // Suggestion xidew to prevent crash if size is null
    if (CGSizeEqualToSize(self.size, CGSizeZero)) {
        return self;
    }

    //    return [other applyBlendFilter:filterOverlay  other:self context:nil];
    // First get the image into your data buffer
    CGImageRef inImage = self.CGImage;
    int nbPerCompt=CGImageGetBitsPerPixel(inImage);
    if(nbPerCompt!=32){
        UIImage *tmpImage=[self normalize];
        inImage=tmpImage.CGImage;
    }
    CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
    UInt8 * m_PixelBuf=malloc(CFDataGetLength(m_DataRef));
    CFDataGetBytes(m_DataRef,
                   CFRangeMake(0,CFDataGetLength(m_DataRef)) ,
                   m_PixelBuf);
   
    CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf,
                                             CGImageGetWidth(inImage),
                                             CGImageGetHeight(inImage),
                                             CGImageGetBitsPerComponent(inImage),
                                             CGImageGetBytesPerRow(inImage),
                                             CGImageGetColorSpace(inImage),
                                             CGImageGetBitmapInfo(inImage)
                                             );
   

    int w=CGImageGetWidth(inImage);
    int h=CGImageGetHeight(inImage);
    int wm=w-1;
    int hm=h-1;
    int wh=w*h;
    int div=radius+radius+1;
   
    int *r=malloc(wh*sizeof(int));
    int *g=malloc(wh*sizeof(int));
    int *b=malloc(wh*sizeof(int));
    memset(r,0,wh*sizeof(int));
    memset(g,0,wh*sizeof(int));
    memset(b,0,wh*sizeof(int));
    int rsum,gsum,bsum,x,y,i,p,yp,yi,yw;
    int *vmin = malloc(sizeof(int)*MAX(w,h));
    memset(vmin,0,sizeof(int)*MAX(w,h));
    int divsum=(div+1)>>1;
    divsum*=divsum;
    int *dv=malloc(sizeof(int)*(256*divsum));
    for (i=0;i<256*divsum;i++){
        dv[i]=(i/divsum);
    }
   
    yw=yi=0;
   
    int *stack=malloc(sizeof(int)*(div*3));
    int stackpointer;
    int stackstart;
    int *sir;
    int rbs;
    int r1=radius+1;
    int routsum,goutsum,boutsum;
    int rinsum,ginsum,binsum;
    memset(stack,0,sizeof(int)*div*3);
   
    for (y=0;y<h;y++){
        rinsum=ginsum=binsum=routsum=goutsum=boutsum=rsum=gsum=bsum=0;
      
        for(int i=-radius;i<=radius;i++){
            sir=&stack[(i+radius)*3];
            /*            p=m_PixelBuf[yi+MIN(wm,MAX(i,0))];
             sir[0]=(p & 0xff0000)>>16;
             sir[1]=(p & 0x00ff00)>>8;
             sir[2]=(p & 0x0000ff);
             */
            int offset=(yi+MIN(wm,MAX(i,0)))*4;
            sir[0]=m_PixelBuf[offset];
            sir[1]=m_PixelBuf[offset+1];
            sir[2]=m_PixelBuf[offset+2];
          
            rbs=r1-abs(i);
            rsum+=sir[0]*rbs;
            gsum+=sir[1]*rbs;
            bsum+=sir[2]*rbs;
            if (i>0){
                rinsum+=sir[0];
                ginsum+=sir[1];
                binsum+=sir[2];
            } else {
                routsum+=sir[0];
                goutsum+=sir[1];
                boutsum+=sir[2];
            }
        }
        stackpointer=radius;
      
      
        for (x=0;x<w;x++){
            r[yi]=dv[rsum];
            g[yi]=dv[gsum];
            b[yi]=dv[bsum];
          
            rsum-=routsum;
            gsum-=goutsum;
            bsum-=boutsum;
          
            stackstart=stackpointer-radius+div;
            sir=&stack[(stackstart%div)*3];
          
            routsum-=sir[0];
            goutsum-=sir[1];
            boutsum-=sir[2];
          
            if(y==0){
                vmin[x]=MIN(x+radius+1,wm);
            }
          
            /*            p=m_PixelBuf[yw+vmin[x]];
            
             sir[0]=(p & 0xff0000)>>16;
             sir[1]=(p & 0x00ff00)>>8;
             sir[2]=(p & 0x0000ff);
             */
            int offset=(yw+vmin[x])*4;
            sir[0]=m_PixelBuf[offset];
            sir[1]=m_PixelBuf[offset+1];
            sir[2]=m_PixelBuf[offset+2];
            rinsum+=sir[0];
            ginsum+=sir[1];
            binsum+=sir[2];
          
            rsum+=rinsum;
            gsum+=ginsum;
            bsum+=binsum;
          
            stackpointer=(stackpointer+1)%div;
            sir=&stack[((stackpointer)%div)*3];
          
            routsum+=sir[0];
            goutsum+=sir[1];
            boutsum+=sir[2];
          
            rinsum-=sir[0];
            ginsum-=sir[1];
            binsum-=sir[2];
          
            yi++;
        }
        yw+=w;
    }
    for (x=0;x<w;x++){
        rinsum=ginsum=binsum=routsum=goutsum=boutsum=rsum=gsum=bsum=0;
        yp=-radius*w;
        for(i=-radius;i<=radius;i++){
            yi=MAX(0,yp)+x;
          
            sir=&stack[(i+radius)*3];
          
            sir[0]=r[yi];
            sir[1]=g[yi];
            sir[2]=b[yi];
          
            rbs=r1-abs(i);
          
            rsum+=r[yi]*rbs;
            gsum+=g[yi]*rbs;
            bsum+=b[yi]*rbs;
          
            if (i>0){
                rinsum+=sir[0];
                ginsum+=sir[1];
                binsum+=sir[2];
            } else {
                routsum+=sir[0];
                goutsum+=sir[1];
                boutsum+=sir[2];
            }
          
            if(i<hm){
                yp+=w;
            }
        }
        yi=x;
        stackpointer=radius;
        for (y=0;y<h;y++){
            //            m_PixelBuf[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum];
            int offset=yi*4;
            m_PixelBuf[offset]=dv[rsum];
            m_PixelBuf[offset+1]=dv[gsum];
            m_PixelBuf[offset+2]=dv[bsum];
            rsum-=routsum;
            gsum-=goutsum;
            bsum-=boutsum;
          
            stackstart=stackpointer-radius+div;
            sir=&stack[(stackstart%div)*3];
          
            routsum-=sir[0];
            goutsum-=sir[1];
            boutsum-=sir[2];
          
            if(x==0){
                vmin[y]=MIN(y+r1,hm)*w;
            }
            p=x+vmin[y];
          
            sir[0]=r[p];
            sir[1]=g[p];
            sir[2]=b[p];
          
            rinsum+=sir[0];
            ginsum+=sir[1];
            binsum+=sir[2];
          
            rsum+=rinsum;
            gsum+=ginsum;
            bsum+=binsum;
          
            stackpointer=(stackpointer+1)%div;
            sir=&stack[(stackpointer)*3];
          
            routsum+=sir[0];
            goutsum+=sir[1];
            boutsum+=sir[2];
          
            rinsum-=sir[0];
            ginsum-=sir[1];
            binsum-=sir[2];
          
            yi+=w;
        }
    }
    free(r);
    free(g);
    free(b);
    free(vmin);
    free(dv);
    free(stack);
    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
    CGContextRelease(ctx);  
   
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);  
    CFRelease(m_DataRef);
    free(m_PixelBuf);
    return finalImage;
}


- (UIImage *) normalize {
   
    CGColorSpaceRef genericColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef thumbBitmapCtxt = CGBitmapContextCreate(NULL,
                                                        
                                                         self.size.width,
                                                         self.size.height,
                                                         8, (4 * self.size.width),
                                                         genericColorSpace,
                                                         kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(genericColorSpace);
    CGContextSetInterpolationQuality(thumbBitmapCtxt, kCGInterpolationDefault);
    CGRect destRect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextDrawImage(thumbBitmapCtxt, destRect, self.CGImage);
    CGImageRef tmpThumbImage = CGBitmapContextCreateImage(thumbBitmapCtxt);
    CGContextRelease(thumbBitmapCtxt);  
    UIImage *result = [UIImage imageWithCGImage:tmpThumbImage];
    CGImageRelease(tmpThumbImage);
   
    return result;  
}


@end
 
 the call will be ...
 self.rawImage.image=[self.rawImage.image stackBlur:10.0f];

the cloud link here

Friday, 14 September 2012

UIToolBar Categories...

Scenario:

Creating the UIToolBar like the image shown....

Solution:

creating the category file for background image of UIToolbar...

.h file :

#import <UIKit/UIKit.h>

@interface UIToolbar (AddtitionalFuntionality)


+(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar;

@end

.m file:

#import "UIToolbar+AddtitionalFuntionality.h"

@implementation UIToolbar (AddtitionalFuntionality)

+(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)bottombar {  
    // Add Custom Toolbar
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
    iv.frame = CGRectMake(0, 0, bottombar.frame.size.width, bottombar.frame.size.height);
    iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    // Add the tab bar controller's view to the window and display.
    if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
        [bottombar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
    else
        [bottombar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
    bottombar.backgroundColor = [UIColor clearColor];
}


@end


The call statement will be of....

[UIToolbar setToolbarBack:@"tool-bar.png" toolbar:toolBarTop];

the source code of the files here
 

UIAlertView category

Scenario:

Create the custom alert view like this....


Solution:

By using the category file [Note: Apple does not allow to   subclass  the UIAlertView ] we can achieve this...

.h file:
#import <UIKit/UIKit.h>

@interface UIAlertView (Custom)
+ (void) setBackgroundColor:(UIColor *) background
            withStrokeColor:(UIColor *) stroke;

- (void) drawRoundedRect:(CGRect) rect inContext:(CGContextRef)
context withRadius:(CGFloat) radius;

+(void) showAlertWithTitile:(NSString*)title withContent:(NSString*)content;
@end
.m file:

#import "UIAlertView+Custom.h"

@implementation UIAlertView (Custom)

static UIColor *fillColor = nil;
static UIColor *borderColor = nil;


+ (void) setBackgroundColor:(UIColor *) background
            withStrokeColor:(UIColor *) stroke
{
    if(fillColor != nil)
    {
        fillColor=nil;
        borderColor=nil;
    }
   
    fillColor = background;
    borderColor = stroke;
}

- (id)initWithFrame:(CGRect)frame
{
    if((self = [super initWithFrame:frame]))
    {
        if(fillColor == nil)
        {
            fillColor = [UIColor blackColor] ;
            borderColor = [UIColor colorWithHue:0.625
                                      saturation:0.0 brightness:0.8 alpha:0.8]
                           ;
        }
    }
   
    return self;
}

- (void)layoutSubviews
{
    for (UIView *sub in [self subviews])
    {
        if([sub class] == [UIImageView class] && sub.tag == 0)
        {
            // The alert background UIImageView tag is 0,
            // if you are adding your own UIImageView's
            // make sure your tags != 0 or this fix
            // will remove your UIImageView's as well!
            [sub removeFromSuperview];
            break;
        }
    }
}

- (void)drawRect:(CGRect)rect
{   
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    CGContextClearRect(context, rect);
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetLineWidth(context, 0.0);
    CGContextSetAlpha(context, 0.8);
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [borderColor CGColor]);
    CGContextSetFillColorWithColor(context, [fillColor CGColor]);
   
    // Draw background
    CGFloat backOffset = 2;
    CGRect backRect = CGRectMake(rect.origin.x + backOffset,
                                 rect.origin.y + backOffset,
                                 rect.size.width - backOffset*2,
                                 rect.size.height - backOffset*2);
   
    [self drawRoundedRect:backRect inContext:context withRadius:8];
    CGContextDrawPath(context, kCGPathFillStroke);
   
    // Clip Context
    CGRect clipRect = CGRectMake(backRect.origin.x + backOffset-1,
                                 backRect.origin.y + backOffset-1,
                                 backRect.size.width - (backOffset-1)*2,
                                 backRect.size.height - (backOffset-1)*2);
   
    [self drawRoundedRect:clipRect inContext:context withRadius:8];
    CGContextClip (context);
   
    //Draw highlight
    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 1.0, 1.0, 1.0, 0.35, 1.0, 1.0, 1.0, 0.06 };
    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace,
                                                        components, locations, num_locations);
   
    CGRect ovalRect = CGRectMake(-130, -115, (rect.size.width*2),
                                 rect.size.width/2);
   
    CGPoint start = CGPointMake(rect.origin.x, rect.origin.y);
    CGPoint end = CGPointMake(rect.origin.x, rect.size.height/5);
   
    CGContextSetAlpha(context, 1.0);
    CGContextAddEllipseInRect(context, ovalRect);
    CGContextClip (context);
   
    CGContextDrawLinearGradient(context, glossGradient, start, end, 0);
   
    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace);
}

- (void) drawRoundedRect:(CGRect) rrect inContext:(CGContextRef) context
              withRadius:(CGFloat) radius
{
    CGContextBeginPath (context);
   
    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
   
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect),
    maxy = CGRectGetMaxY(rrect);
   
    CGContextMoveToPoint(context, minx, midy);
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
    CGContextClosePath(context);
}


+(void) showAlertWithTitile:(NSString*)title withContent:(NSString*)content
{
    UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:title
                                                       message: content
                                                      delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] ;
   
    [theAlert show];
   
    UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];
    [theTitle setTextColor:[UIColor redColor]];
   
    UILabel *theBody = [theAlert valueForKey:@"_bodyTextLabel"];
    [theBody setTextColor:[UIColor blueColor]];
   
    UIImage *theImage = [UIImage imageNamed:@"pattern2.jpg"];//nil;   
    theImage = [theImage stretchableImageWithLeftCapWidth:16 topCapHeight:16];
    CGSize theSize = [theAlert frame].size;
   
    UIGraphicsBeginImageContext(theSize);   
    [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];   
    theImage = UIGraphicsGetImageFromCurrentImageContext();   
    UIGraphicsEndImageContext();
   
    CGImageRef img=theImage.CGImage;
    [[theAlert layer] setContents: ( id)img];
}

@end
Call will be.....

UIAlertView *alert=[[UIAlertView alloc ]initWithTitle:@"Welcome To Category" message:@"Hi" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [UIAlertView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"red.jpg"]] withStrokeColor:[UIColor magentaColor]];
    [alert show];
    [alert release];

The source code available here

Age calculation from the given NSDate using categories

Hi friends i glad to share my ideas and my workouts here.....

Scenario :
      Need to calculate the age of the person....
 
 category file content will be of this.....
           .h file code will be :-

#import <Foundation/Foundation.h>

@interface NSDate (AdditionalMethods)

- (NSInteger)age;

@end

.m file code will be :-
#import "NSDate+AdditionalMethods.h"

@implementation NSDate (AdditionalMethods)


- (NSInteger)age {
   
    NSCalendar *calendar = [NSCalendar currentCalendar];
   
    unsigned flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
   
    NSDateComponents *dateComponentsNow = [calendar components:flags fromDate:[NSDate date]];
    NSDateComponents *dateComponentsBirth = [calendar components:flags fromDate:self];
   
    if (([dateComponentsNow month] < [dateComponentsBirth month]) || (([dateComponentsNow month] == [dateComponentsBirth month]) && ([dateComponentsNow day] < [dateComponentsBirth day]))) {
        return [dateComponentsNow year] - [dateComponentsBirth year] - 1;
    } else {
        return [dateComponentsNow year] - [dateComponentsBirth year];
    }
}


@end


The method call will be....

    NSString *dateStr = @"20081122";
   
    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyyMMdd"];
    NSDate *date = [dateFormat dateFromString:dateStr]; 
   
    // Convert date object to desired output format
    [dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
    dateStr = [dateFormat stringFromDate:date]; 
    [dateFormat release];
    NSLog(@"Age now: %d",date.age);



you may download from here