2011-11-30 141 views
2

我申请的背景图像我的ViewController:MonoTouch。旋转背景图片

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

现在,当屏幕方向得到改变它breakes我的整个背景的东西,因为那画面保持原样。当然,我可以在Photoshop中旋转图像并将其放入我的项目中,但我对软件工程师的谦逊自豪感起了反感。

我通过了很多来源搜索。我已经尝试了Objective-C样本。我在c#中只找到了一些。我没有时间学习UIKit和Core Graphics之间的差异。我试过CGContext.RotateCTM,我试图用CGAffineTransform.MakeRotation来实现。它不起作用。我只需要做一件简单的事情。

显然在使用RotateCTM或更改CGAffineTransform之前,您必须以某种方式定义关键点。

请有人给我看一个简单的例子,它是如何工作的。

UPD:

这是我走到这一步:

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

,正如你可以看到它是不是没有好,尽管它旋转:

portraitlandscape

我错过了什么?

回答

6

添加一个UIImageView作为您的控制器视图的子视图并将您的图像加载到该子视图中。 您可能需要将UIImageViewContentMode设置为ScaleFit以调整其大小。 将您的UIImageViewAutoresizingMask设置为FlexibleWidthFlexibleHeight,并且您应该获得所需的结果和旋转(只要您的控制器覆盖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; 
      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; 
     } 
    } 
} 

enter image description here enter image description here

+0

没有它不工作。我想当设备改变其方向时,它也应该旋转图像90度。它不 – Agzam

+0

你试过了吗?我已经添加了一些代码来证明它正在按照我向您解释的方式工作。 – Krumelur

+0

对不起......显然我尝试了错误的方式......我的道歉。感谢您提供详细的示例代码。 – Agzam

4

这可能会成为一个合格的不当使用FromPatternImage的。在这种情况下,生成的“UIColor”不是视图,也不符合接收任何旋转事件的条件。因此,您可能能够计算边界更改,旋转图像,应用新计算的边界,重置所有内部的BackgroundColor属性(全部都在WillRotateToInterfaceOrientation(...)内),但这非常复杂,甚至不具有远程执行效果。

在我看来,更理智的路径是简单地创建一个UIImageView并将其置于层次结构中的一切:

var imageView = new UIImageView(UIImage.FromFile("Images/background.png")); 
imageView.Frame = new RectangleF(...) // set appropriate frame here 
imageView.AutoResizingMask = ...; // set appropriate mask here 
ParentViewController.View.AddSubView(imageView); 
ParentViewController.View.SendSubviewToBack(imageView); 

这使系统承担的东西负责它的设计做的,并保持你不必编写昂贵的旋转代码,同时达到预期的效果。

+0

你的意思是我必须有一个肖像模式的图像和另一个景观? – Agzam

+0

这就是我已经提出的,但海报宣称UIIMageView不起作用。我已经更新了我的回答,以证明相反。 – Krumelur