Wednesday, 29 April 2015

image compression by size

CGFloat compression = 0.9f;
    CGFloat maxCompression = 0.1f;
    int maxFileSize = 250*1024;
    
    NSData *imageData = UIImageJPEGRepresentation(yourimage, compression);
    
    while ([imageData length] > maxFileSize && compression > maxCompression)
    {
        compression -= 0.1;
        imageData = UIImageJPEGRepresentation(yourimage, compression);

    }

Wednesday, 22 April 2015

UIViewController default back button action

UIViewController+BackButtonHandler.h

#import <UIKit/UIKit.h>

@protocol BackButtonHandlerProtocol <NSObject>
@optional

-(BOOL)navigationShouldPopOnBackButton;
@end

@interface UIViewController (BackButtonHandler) <BackButtonHandlerProtocol>

@end

UIViewController+BackButtonHandler.m

#import "UIViewController+BackButtonHandler.h"

@implementation UIViewController (BackButtonHandler)

@end

@implementation UINavigationController (ShouldPopOnBackButton)

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

if([self.viewControllers count] < [navigationBar.items count]) {
return YES;
}

BOOL shouldPop = YES;
UIViewController* vc = [self topViewController];
if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
shouldPop = [vc navigationShouldPopOnBackButton];
}

if(shouldPop) {
dispatch_async(dispatch_get_main_queue(), ^{
[self popViewControllerAnimated:YES];
});
} else {
for(UIView *subview in [navigationBar subviews]) {
if(subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
}

return NO;
}

@end

put this code in your child view controller 

#import "UIViewController+BackButtonHandler.h"

-(BOOL) navigationShouldPopOnBackButton
{
[[[UIAlertView alloc] initWithTitle:@"Confirmation" message:@"Are you sure you want to go back?"
  delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil] show];
return NO;
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1) {
[self.navigationController popViewControllerAnimated:YES];
}
}


Pass value to parent controller when popview(or)dismiss the controller

Put this code in your parent controller

- (void)viewDidLoad {
    [super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(BackButtonNotification:)
                                      name:@"POPVIEW" object:nil];

(OR)

[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(BackButtonNotification:)
                                      name:@"DISMISS" object:nil];
 }


-(void)BackButtonNotification:(NSNotification *)notice{
    NSString *value = [notice object];
    NSLog(@"value: %@",value);
}

Put this code in your child controller back button action place

-(void)navigationShouldPopOnBackButton{

   [self.navigationController popViewControllerAnimated:YES];

    [[NSNotificationCenter defaultCenter]
          postNotificationName:@"POPVIEW"
                        object:@"You pass value to parent class"];
}

                    (OR)
                 
-(void)dissmissModelView{

    [self dismissModalViewControllerAnimated:YES];

 [[NSNotificationCenter defaultCenter]
          postNotificationName:@"DISMISS"
                        object:@"You pass value to parent class"];
}

Wednesday, 8 April 2015

set cornerRadius for only bottom-left,bottom-right and top-left corner of a view/cell


UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(20.0, 20.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
(0r)
 cell.layer.mask = maskLayer;

Tuesday, 7 April 2015

NSDictionary, NSMutableArray ascending order

  NSMutableArray *contacts;

  NSMutableArray *contactsDisplayobject;

BEFORE OUTPUT:
contacts: (
              {
        Email = "";
        fName = Hank;
        lName = Zakroff;
        numberVal = 7075551854;
    },
        {
        Email = "";
        fName = Cheesy;
        lName = Cat;
        numberVal = 2015552398;
    },
        {
        Email = "";
        fName = Freckles;
        lName = Dog;
        numberVal = 3331560987;
    }
)

NSSortDescriptor *NameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:fname ascending:YES];  // Sort by name
contactsDisplayobject = [[NSMutableArray alloc] initWithArray:[contacts sortedArrayUsingDescriptors:@[NameSortDescriptor]]];

AFTER OUTPUT:

contactsDisplayobject: (
        {
        Email = "";
        fName = Cheesy;
        lName = Cat;
        numberVal = 2015552398;
    },
        {
        Email = "";
        fName = Freckles;
        lName = Dog;
        numberVal = 3331560987;
    },
       {
        Email = "";
        fName = Hank;
        lName = Zakroff;
        numberVal = 7075551854;
    }
)

Monday, 6 April 2015

when the 'back' button is pressed on a navbar

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (self.isMovingFromParentViewController) {
        // Do your stuff here

    }

}

Wednesday, 1 April 2015

Show only numbers

array = ( 415, 416)

NSString *arr_item = [NSString stringWithFormat:@"%@",array];

NSString *condensedPhoneno = [[arr_item componentsSeparatedByCharactersInSet:
                                       [[NSCharacterSet characterSetWithCharactersInString:@",0123456789"]
                                        invertedSet]]
                                      componentsJoinedByString:@""];

NSLog(@"result : %@",condensedPhoneno);

result: 415,416