iOS - Image View



Use of Image View

Image view is used for displaying a single image or 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 a 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 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 have to add images named as "AppleUSA1.jpg" and "AppleUSA2.jpg" to our project, which can be done by dragging the image to our navigator area where our project files are listed.

Update viewDidLoad in ViewController.m as follows −

(void)viewDidLoad {
   [super viewDidLoad];
   [self addImageView];
}

Output

When we run the application, we'll get the following output −

iOS Tutorial

You can try calling addImageViewWithAnimation instead of addImageView method to see the animation effect of image view.

ios_ui_elements.htm
Advertisements