iOS - Image View

Advertisements


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 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

Now 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.



Advertisements
Advertisements