Use of Image Views
Image views are used to display a single image or an animated sequence of images.
Important Properties
- image
- highlightedImage
- userInteractionEnabled
- animationImages
- animationRepeatCount
Important Methods
- (id)initWithImage:(UIImage *)image
- (id)initWithImage:(UIImage *)image highlightedImage:
(UIImage *)highlightedImage
- (void)startAnimating
- (void)stopAnimating
Add the custom method addImageView
-(void)addImageView{
UIImageView *imgview = [[UIImageView alloc]
initWithFrame:CGRectMake(10, 10, 300, 400)];
[imgview setImage:[UIImage imageNamed:@"AppleUSA1.jpg"]];
[imgview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imgview];
}
Add another custom method addImageViewWithAnimation
This method explains how to animate the images in imageView
-(void)addImageViewWithAnimation{
UIImageView *imgview = [[UIImageView alloc]
initWithFrame:CGRectMake(10, 10, 300, 400)];
// set an animation
imgview.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"AppleUSA1.jpg"],
[UIImage imageNamed:@"AppleUSA2.jpg"], nil];
imgview.animationDuration = 4.0;
imgview.contentMode = UIViewContentModeCenter;
[imgview startAnimating];
[self.view addSubview:imgview];
}
Note: We must add images named "AppleUSA1.jpg" and "AppleUSA2.jpg" to our project, which can be done by dragging the images into our navigation area, which lists our project files.
Update viewDidLoad in ViewController.m as follows
(void)viewDidLoad
{
[super viewDidLoad];
[self addImageView];
}