Monday, March 25, 2013

fast Downloading Images From server and save to document directory

Add New Class of type NSobject :-
.H file :-


#import <Foundation/Foundation.h>

@interface downloadImageClass : NSObject{

}
+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage;


@end


.M file :-

#import "downloadImageClass.h"
#import <QuartzCore/QuartzCore.h>

@implementation downloadImageClass

+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage
{
    NSURL *url = [NSURL URLWithString:urlString];
    
    dispatch_queue_t callerQueue = dispatch_get_current_queue();
    dispatch_queue_t downloadQueue = dispatch_queue_create("com.myapp.processsmagequeue", NULL);
    dispatch_async(downloadQueue, ^{
        NSData * imageData = [NSData dataWithContentsOfURL:url];
        
        dispatch_async(callerQueue, ^{
            processImage(imageData);
        });
    });
    dispatch_release(downloadQueue);
}




@end



Save Images to document Directory  :-


- (void)save_to_document_directory{

    NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathArr objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *dirPath1 = [path stringByAppendingPathComponent:@"FolderNserverame"];
    self.dirPath = dirPath1;
if (![fileManager fileExistsAtPath:dirPath1]) {
[fileManager createDirectoryAtPath:dirPath1 withIntermediateDirectories:NO attributes:nil error:nil];
}
NSError *error;
for (NSString *file in [fileManager contentsOfDirectoryAtPath:dirPath1 error:&error]) {
BOOL success = [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirPath1, file] error:&error];
if (!success || error) {
// it failed.
}
}
    
    for (int i = 0 ; i < [myimagesArray count]; i++) {
        [WebImageOperations processImageDataWithURLString:[myimagesArray objectAtIndex:i] andBlock:^(NSData *imageData) {
          
            
                NSString *imageName = [[[self.responseDataDic objectAtIndex:i]objectForKey:@"Name"]stringByAppendingString:@".png"];
                NSLog(@"imageName....%@",imageName);
                if (![fileManager fileExistsAtPath:[dirPath1 stringByAppendingPathComponent:imageName]]) {
                    [fileManager createFileAtPath:[dirPath1 stringByAppendingPathComponent:imageName] contents:imageData attributes:nil];
                }
           

            
        }];
    }
    
}

Sunday, March 24, 2013

Facebook login dialog with UIAlertView


Because at the clickedButtonAtIndex function the view isn't closed and this can cause problems.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

}

So write FBLogin Method in:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

[Self showFBLoginDialog];

}

Monday, March 18, 2013

UIRefreshControl in iOS


UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(DoRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.myTableView addSubview:refreshControl];

Wednesday, March 13, 2013

Save Image to document directory with timestamp ..ios


NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
     NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
    
    NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage%@.jpg",timeStampObj]];

Taking High resolution screenshot for ratina display ... iOS


CGRect screenRect = CGRectMake(0.0f, 44.0f,320.0f, 424.0f);


//High Resolution ScreenShot (640*848)
 UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque,  [[UIScreen mainScreen] scale]);
     [view.layer renderInContext:UIGraphicsGetCurrentContext()];
     
     UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
     
     UIGraphicsEndImageContext();


//Low Resolution ScreenShot (320*424)


UIGraphicsBeginImageContext(screenRect.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
    [self.view.layer renderInContext:ctx];
screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Stop Image Rotation While Using Camera....by 90 degree


- (UIImage *)scaleAndRotateImage:(UIImage *)image {
    int kMaxResolution = 640
    
    CGImageRef imgRef = image.CGImage;
    
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
    
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    if (width > kMaxResolution || height > kMaxResolution) {
        CGFloat ratio = width/height;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = roundf(bounds.size.width / ratio);
        }
        else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = roundf(bounds.size.height * ratio);
        }
    }
    
    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {
            
        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;
            
        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;
            
        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;
            
        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
            
        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
            
        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
            
        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
            
        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
            
    }
    
    UIGraphicsBeginImageContext(bounds.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }
    
    CGContextConcatCTM(context, transform);
    
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imageCopy;
}

///Method in apple documentation.... :)

Wednesday, March 6, 2013

Phone Number Validation in iPhone SDK


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

  int length = [self getLength:textField.text];

  if(length == 10)
  {
    if(range.length == 0)
        return NO;
  }

  if(length == 3)
  {
    NSString *num = [self formatNumber:textField.text];
    textField.text = [NSString stringWithFormat:@"(%@) ",num];
    if(range.length > 0)
        textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
  }
  else if(length == 6)
  {
    NSString *num = [self formatNumber:textField.text];
    
    textField.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];
    if(range.length > 0)
        textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
  }

  return YES;
}

-(NSString*)formatNumber:(NSString*)mobileNumber
{

mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];


int length = [mobileNumber length];
if(length > 10)
{
    mobileNumber = [mobileNumber substringFromIndex: length-10];

}


return mobileNumber;
}


-(int)getLength:(NSString*)mobileNumber
{

  mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

int length = [mobileNumber length];

return length;


}