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?
Add a
UIImageViewas subview to your controller’s view and load your image into that subview.You might want to set the
ContentModeof theUIImageViewtoScaleFitto make it resize.Set the
AutoresizingMaskof yourUIImageViewtoFlexibleWidthandFlexibleHeightand you should get the desired result and rotation (as long as your controller overrideShouldAutorotateToOrientation()).EDIT SAMPLE CODE: