Monday, October 25, 2010

Display image in UIImageView



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(2047283312)];
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


i Hope this post was helpful for you, Happy iCoding

No comments:

Post a Comment