Search This Blog

Thursday, 1 March 2012

Display image in UIImageView in iPhone




Hi Friends,


In this tutorial you will see how to use a UIImageView class, let's get started 

Step 1: Create a window based application and name it ImageDemo and then add a viewController subclass files and name it Myimageviewcontroller so now your application bundle should look like this



Step 2: Create an object of UIImageView class in the .h file 



@interface Myimageviewcontroller : UIViewController {
 UIImageView *obj_imageView;
}
@end

Step 3: Go to the .m file and add the following piece of code.

[obj_imageView setFrame:CGRectMake(20, 47, 283, 312)];
obj_imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"AppleTV.jpg"]];


In the above piece of code i have set the frame of the UIImageView and i have loaded the imageview with a default image that i have took in the Resource folder of my project.

[For this code to work you must have an image present into your project].

Step 4: Go to the ImageDemoAppDelegate.m  file and add the view to the window with the help of the given code


#import "ImageDemoAppDelegate.h"
#import "Myimageviewcontroller.h"

@implementation ImageDemoAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {    

     Myimageviewcontroller *objviewcontroller = [[Myimageviewcontroller alloc]init];
 [window addSubview:objviewcontroller.view];
       [window makeKeyAndVisible];
}

Step 5: Build and run the application you will get the below output on the iPhone simulator

Final Output

No comments:

Post a Comment