Fetch image from database in Iphone


In this tutorial we will see the app is how we can fetch the image and other data from the database . This is the navigation  based app.Here are some steps for it…



1. Open the xcode & choose "File->New Project".

2. Select "Application" from left menu and then "View-based Application".

3.Name your project as "FetchImage" and save the project.

4.Now just select "RootViewController.h" file from left menu.


 

#import <UIKit/UIKit.h>

#import"DetailViewController.h"


@interface RootViewController : UITableViewController {

DetailViewController *DetailView;

}

@property(nonatomic,retain)DetailViewController *DetailView;


@end

5. Now select "RootViewController.m" file from menu for implementation.

#import "RootViewController.h"

#import "FetchImageAppDelegate.h"

#import "Discription.h"

#import "DetailViewController.h"

@implementation RootViewController

@synthesize DetailView;


#pragma mark -

#pragma mark View lifecycle



- (void)viewDidLoad {

    [super viewDidLoad];


    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.title=@"ITEMS";

}





#pragma mark -

#pragma mark Table vew data source


// Customize the number of sections in the table view.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}



// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    

FetchImageAppDelegate *fatch=(FetchImageAppDelegate *)[[UIApplication sharedApplication] delegate];


return fatch.items.count;

}



// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    

// Configure the cell.

FetchImageAppDelegate *fatch=(FetchImageAppDelegate *)[[UIApplication sharedApplication] delegate];

Discription *dish=(Discription*)[fatch.items objectAtIndex:indexPath.row];

cell.textLabel.text=dish.name;


    return cell;

}





#pragma mark -

#pragma mark Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    

FetchImageAppDelegate *fatch=(FetchImageAppDelegate*)[[UIApplication sharedApplication] delegate];

Discription *dish=(Discription*)[fatch .items objectAtIndex:indexPath.row];

if(self.DetailView==nil){

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

self.DetailView=detailViewController;

[detailViewController release];

}

// ...

     // Pass the selected object to the new view controller.

[self.navigationController pushViewController:self.DetailView animated:YES];

self.DetailView.title=[dish name];

[self.DetailView.discri setText:[dish discription]];

NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:[dish image]]];

  UIImage *itemImage=[[UIImage alloc]initWithData:imageData cache:YES] ;

  self.DetailView.image.image=itemImage;

}



- (void)dealloc {

    [super dealloc];

}



@end

6. Now select "FetchImageAppDelegate.m" file from menu for implementation. Here we create and setup the database methods.

 

#import "FetchImageAppDelegate.h"

#import "RootViewController.h"


@implementation FetchImageAppDelegate


@synthesize window;

@synthesize navigationController,databaseName,dataBasePath,items,rowid;



#pragma mark -

#pragma mark Application lifecycle


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    databaseName=@"Details.sqlite";

NSArray *documentPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *dirPath=[documentPath objectAtIndex:0];

dataBasePath=[dirPath stringByAppendingPathComponent:databaseName];

NSLog(@"%@",dataBasePath);

[self checkAndCreatedatabase];

[self readFromDatabase];

    // Override point for customization after application launch.

    

    // Add the navigation controller's view to the window and display.

    [window addSubview:navigationController.view];

    [window makeKeyAndVisible];


    return YES;

}

-(void)checkAndCreatedatabase{

BOOL success;

NSFileManager *fileManger=[NSFileManager defaultManager];

success =[fileManger fileExistsAtPath:dataBasePath];

if(success)

return;

NSString *databasePathFromApp=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:databaseName];

[fileManger copyItemAtPath:databasePathFromApp toPath:dataBasePath error:nil];

[fileManger release];


}


-(void)readFromDatabase{

sqlite3 *database;

items =[[NSMutableArray alloc]init];

if (sqlite3_open([dataBasePath UTF8String], &database)==SQLITE_OK)

{

const char *sqlStatment="select * from Item";

sqlite3_stmt *compiledStatment;

if(sqlite3_prepare(database, sqlStatment, -1, &compiledStatment,nil)==SQLITE_OK)

{

while (sqlite3_step(compiledStatment)==SQLITE_ROW) {

NSString *name=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatment, 0)];

NSString *discription=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatment, 1)];

NSString *image=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatment, 2)];

Discription *dish=[[Discription alloc] initWithData:name discription:discription image:image ];

[items addObject:dish];

[dish release];

}

}

sqlite3_finalize(compiledStatment);

}


sqlite3_close(database);


}


- (void)dealloc {

[navigationController release];

[window release];

[super dealloc];

}



@end

 

7. Now double click on "RootViewController.xib" from Resource folder for open it.


8. Then Quit and Save the Interface Builder.


9. Now you are ready for  click on "Build & Go" button. 


Now you can see the output in iphone simulator .

 

 

 

 

 



(5) Responses to this post

Kelli May 05 2011 at 17:14:19

It's spooky how clever some ppl are. Thkans!

youffzic May 05 2011 at 21:48:54

aCyU0I zxuavpmsyokk

vosrbc May 07 2011 at 05:37:16

qP2wqb , [url=http://ocoutyyymcij.com/]ocoutyyymcij[/url], [link=http://afbrejkpejhq.com/]afbrejkpejhq[/link], http://xzxenjbfineh.com/

Anirudh Nov 03 2011 at 00:16:36

This is the navigation based app but u r clickin on view based..

valium Mar 15 2012 at 06:38:05

http://www.shopdiazepamfast.com/ - valium online no prescription buy cheap valium online no prescription valium online generic valium online http://www.shopdiazepamfast.com/


Leave your comment here






User Login




New User Forgot password

Newsletter Subscribe

Advertisement

 
 
 
quick contact