MonoTouch. Rotating background image

I've applied a background image to my ViewController:

ParentViewController.View.BackgroundColor = UIColor.FromPatternImage (image)

Now, when screen orientation gets changed it breakes my entire background thing, because that picture stays as it was. Of course I could just rotate the image in Photoshop and put it in my project, but my humble pride of software engineer revolted.

I've searched through many sources. I've tried objective-c samples. I find only a few in c#. I don't have any time to learn differences between UIKit and Core Graphics. I've tried CGContext.RotateCTM, I've tried to achieve that with CGAffineTransform.MakeRotation. It doesn't work. I just need a simple thing to be done.

Apparently before using RotateCTM or changing CGAffineTransform you have to somehow define the pivotal point.

Please somebody show me a simple example, how it works.


This is what I got so far:

var image = new UIImage ("Images/background.jpg");
if (interfaceOrientation == UIInterfaceOrientation.LandscapeLeft) {
   CGImage imageRef = image.CGImage;
   var width = imageRef.Width;
   var height = imageRef.Height;
   CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo;
   CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB ();
   CGBitmapContext bitmap = 
           new CGBitmapContext (IntPtr.Zero, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);

   bitmap.TranslateCTM(0, imageRef.Height);
   bitmap.DrawImage (new Rectangle (0, 0, height, width), imageRef);
   image = UIImage.FromImage (bitmap.ToImage());
   bitmap = null;
ParentViewController.View.BackgroundColor = UIColor.FromPatternImage (image);

and as you can see it ain't no good, though it does rotate:

What am I missing?


Add a UIImageView as subview to your controller's view and load your image into that subview. You might want to set the ContentMode of the UIImageView to ScaleFit to make it resize. Set the AutoresizingMask of your UIImageView to FlexibleWidth and FlexibleHeight and you should get the desired result and rotation (as long as your controller override ShouldAutorotateToOrientation()).

var imageView = new UIImageView( UIImage.FromFile( pathToYourImage ) );


using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Rotate
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
        // class-level declarations
        UIWindow window;

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
            // create a new window instance based on the screen size
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            window.RootViewController = new TestController();
            // make the window visible
            window.MakeKeyAndVisible ();

            return true;

    public class TestController : UIViewController
        public TestController() : base()

        public override void LoadView ()
            base.LoadView ();
            this.imgView = new UIImageView(UIImage.FromFile("img.png"));
            imgView.Frame = new System.Drawing.RectangleF(0, 0, 300, 300);
            this.imgView.ContentMode = UIViewContentMode.ScaleAspectFit;
            this.imgView.AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin;
        private UIImageView imgView;

        public override void ViewWillAppear (bool animated)
            this.imgView.Center = new System.Drawing.PointF(this.View.Bounds.Width / 2, this.View.Bounds.Height / 2);
            base.ViewWillAppear (animated);

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
            return true;

Need Your Help

Parse string using Regular expression


i have this kind of string File type: Wireshark - pcapng

Email Validation Using Loops

java email-validation

I have a question regarding Email validation with Java. I know that the right way of using it is by regular expression and patterns, but for some reason I have been asked to write it using loop. In...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.