Sunday, July 3, 2011

ImageLoad from Server in iPhone

In this application we will see how to image fetch from the server and place it to the view. I will show you the easiest way for image fetch from server. So let see how it will work.

Step 1: Open the Xcode, Create a new project using Window base application. Give the application “imageLoad”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add one ViewController class in the project. So select the project -> New File -> Cocoa Touch ->ViewController class and give the class name “ImageLoadFromServer”.

Step 4: Open the ImageLoadAppDelegate.h file, we need to define ImageLoadFromServer class and create an instance of ImageLoadFromServer class. So make the following changes in the file.

#import
@class ImageLoadFromServer;
@interface ImageLoadAppDelegate : NSObject {
ImageLoadFromServer *imageLoadFromServer;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ImageLoadFromServer *imageLoadFromServer;

@end

Step 5: Now make the following changes in the ImageLoadAppDelegate.m file.

#import "ImageLoadAppDelegate.h"
#import "ImageLoadFromServer.h"
@implementation ImageLoadAppDelegate

@synthesize window=_window;
@synthesize imageLoadFromServer;

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

imageLoadFromServer = [[ImageLoadFromServer alloc] init];
[_window addSubview:imageLoadFromServer.view];
[self.window makeKeyAndVisible];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

}

- (void)applicationWillTerminate:(UIApplication *)application
{

}

- (void)dealloc
{
[_window release];
[super dealloc];
}

@end

Step 6: Open the ImageLoadFromServer.h file and create an instance of UIImageView. So make the following changes in the file:

#import
@interface ImageLoadFromServer : UIViewController {

UIImageView *imageLoad;

}
@property (nonatomic, retain) IBOutlet UIImageView *imageLoad;
@end

Step 7: In the ImageLoadFromServer.m file make the following changes:

#import "ImageLoadFromServer.h"
@implementation ImageLoadFromServer
@synthesize imageLoad;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}

return self;
}

- (void)dealloc
{
[imageLoad release];
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

#pragma mark – View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL *imageurl = [NSURL URLWithString:@"http://www.chakrainteractive.com/mob/ImageUpoad/pic2-2.png"];
NSData *imagedata = [[NSData alloc]initWithContentsOfURL:imageurl];
UIImage *image = [UIImage imageWithData: imagedata];
imageLoad = [[UIImageView alloc] initWithImage: image];

[self.view addSubview:imageLoad];

}

- (void)viewDidUnload
{
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 8: Now compile and run the application in the Simulator.



You can Download SourceCode from here ImageLoad

No comments:

Post a Comment

Followers