Using the Image View

An image view is used to display a single image or an image of an animated sequence.

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 the 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. This can be done by dragging the images to our navigator area, where the project files are listed.

Update viewDidLoad in ViewController.m as shown below

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