Image view is used for displaying a single image or animated sequence of images.
- (id)initWithImage:(UIImage *)image - (id)initWithImage:(UIImage *)image highlightedImage: (UIImage *)highlightedImage - (void)startAnimating - (void)stopAnimating
-(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]; }
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]; }
When we run the application, we'll get the following output −
You can try calling addImageViewWithAnimation instead of addImageView method to see the animation effect of image view.