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.

Upd:

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.RotateCTM((float)-1.5707f);
   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?

Answers


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

EDIT SAMPLE CODE:

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;
            this.View.AddSubview(this.imgView);
        }
        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

PPC breakpoints

debugging osx assembly

How is a breakpoint implemented on PPC (On OS X, to be specific)?

Displaying a message in a dialog box using AJAX, jQuery, and CakePHP

ajax jquery-ui cakephp jquery

I have a form, and when users submit this form, it should pass the data along to a function using AJAX. Then, the result of that is displayed to the user in a dialog box. I'm using CakePHP (1.3) and

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.