Xamarin - Documentation

iOS

change platform

  1. Xamarin
  2. iOS
  3. Recipes
  4. Media
  5. Images
  6. Rotate An Image

Rotate An Image

Table of contents

This recipe shows how to rotate an image on the screen using a UIImageView and a CGAffineTransform.

Sample Code:

RotateImage.zip

Related Articles:

Working with Images

Related Apple Documentation:

Quartz 2D Programming Guide

Recipe

RotateImage.png

  1. Add a sample image named monkey.png.
  2. In a UIViewController subclass, add class variables for a UIImage and UIImageView.
UIImage _image;
UIImageView _imageView;
  1. In the ViewDidLoad method create the UIImage and the UIImageView.
_image = UIImage.FromFile("monkey.png");
_imageView = new UIImageView(new RectangleF(50,50,100,100));
_imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
_imageView.Image = _image;
  1. Set the Transform property of the UIImageView and add it as a sub view.
_imageView.Transform = CGAffineTransform.MakeRotation((float)Math.PI/4);            
View.AddSubview (_imageView);

Additional Information

The CGAffineTransform has helper functions to return various affine transforms such as rotation, scale and translation. Setting a CGAffineTransform to the Transform property of a UIView applies the transformation matrix to the view.