Wednesday, June 26, 2013

Store image in Document Directory and Retrieve it in iOS

In this Example i will show how to store image in document directory and Retrieve it.

Following Method we will use to store.

1.NSSearchPathForDirectoriesInDomains : Creates a list of directory search paths. Creates a list of path strings for the specified directories in the specified domains. The list is in the order in which you should search the directories. If expandTilde is YES, tildes are expanded as described in stringByExpandingTildeInPath. You should consider using the NSFileManager methods URLsForDirectory:inDomains: and URLForDirectory:inDomain:appropriateForURL:create:error:. which return URLs, which are the preferred format.

Let Start
here imgData is in NSData Format.

 >>Storing image in document Directory.


NSData *imgData=UIImagePNGRepresentation([UIImage imageNamed:@"Test.png"]);


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            
            NSString *localFilePath= [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"frame_%d.jpeg",1]];
            NSLog(@"local file path at save --------------- %@",localFilePath);
[imgData writeToFile:localFilePath atomically:YES];

 >> Retrieve Image from document Directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath= [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"frame_%d.jpeg",1]];
    


UIImage *givenImage = [UIImage imageWithContentsOfFile:localFilePath];


NSData *myimg =  UIImageJPEGRepresentation(givenImage,0.5);
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
img.image=[UIImage imageWithData:myimg];
[self.view addSubview:img];


            


Sunday, June 16, 2013

Get Size and Resolution of iOS Device

In the Example I will show you how you get Actual Pixel Resolution and size of iOS Device by coding.

Just you have to find the Screen size bound and scale.


CGRect rect=[[UIScreen mainScreen] bounds];
    CGFloat scale=[[UIScreen mainScreen] scale];
    
    NSLog(@"Actual Pixel Resolution: width :% f,height :%f",rect.size.width * scale,rect.size.height * scale);
     NSLog(@" Actual Size width :% f,height :%f",rect.size.width ,rect.size.height );

O/P
For retina 4-inch display
Actual Pixel Resolution: width : 640.000000,height :1136.000000
Actual Size width : 320.000000,height :568.000000


Monday, June 10, 2013

Move or Delete UiTableViewCell in iOS

In this example i will show how to move or delete cell in TableView.
First take UiTableView, allocate it datasource and delegate to File's Owner.

.h

#import <UIKit/UIKit.h>

@interface FavouriteListVC : UITableViewController
-(IBAction)btnEdit:(id)sender;
@end


.m
ViewDidLoad.


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *btn=[[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(btnEdit:)];
    btn.tag=10;
    self.navigationItem.rightBarButtonItem=btn;

}
-(IBAction)btnEdit:(id)sender
{
    int tag=[sender tag];
    if(tag==10)
    {
    [self.tableView setEditing:YES animated:YES];
    [sender setTitle:@"Done"];
        self.editing=YES;
        [sender setTag:11];
    }
    else
    {
        [self.tableView setEditing:NO animated:YES];
        [sender setTitle:@"Edit"];
        [sender setTag:10];
    }
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSObject *obj=[self.favList objectAtIndex:sourceIndexPath.row];
if(destinationIndexPath.row>sourceIndexPath.row)
{
    for(int x=destinationIndexPath.row;x>sourceIndexPath.row;x--)
    {
        [self.favList replaceObjectAtIndex:x-1 withObject:[self.favList objectAtIndex:x]];
    }
}
    else
    {
        for(int x=destinationIndexPath.row;x<sourceIndexPath.row;x++)
        {
            [self.favList replaceObjectAtIndex:x+1 withObject:[self.favList objectAtIndex:x]];
        }
    }
    [self.favList replaceObjectAtIndex:destinationIndexPath.row withObject:obj];
    
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle==UITableViewCellEditingStyleDelete)
{
    [self.favList removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:

[NSArray arrayWithObject:indexPath]
         withRowAnimation:UITableViewRowAnimationFade];
   
}

}