我已将背景图像应用到我的 ViewController:
ParentViewController.View.BackgroundColor = UIColor.FromPatternImage (image)
现在,当屏幕方向改变时,它破坏了我的整个背景,因为那张照片保持原样。当然,我可以在 Photoshop 中旋转图像并将其放入我的项目中,但我作为软件工程师的卑微自尊心受到了反抗。
我搜索了很多资源。我试过 objective-c 样本。我在 c# 中只找到了几个。我没有时间学习 UIKit 和 Core Graphics 之间的区别。我试过 CGContext.RotateCTM
,我尝试用 CGAffineTransform.MakeRotation
来实现。它不起作用。我只需要完成一件简单的事情。
显然,在使用 RotateCTM
或更改 CGAffineTransform
之前,您必须以某种方式定义关键点。
请有人告诉我一个简单的例子,它是如何工作的。
更新:
这是我到目前为止得到的:
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);
如您所见,它并不是什么好东西,尽管它确实会旋转:
我错过了什么?
最佳答案
将 UIImageView
作为 subview 添加到 Controller 的 View 中,并将图像加载到该 subview 中。
您可能希望将 UIImageView
的 ContentMode
设置为 ScaleFit
以使其调整大小。
将 UIImageView
的 AutoresizingMask
设置为 FlexibleWidth
和 FlexibleHeight
,您应该会得到所需的结果和旋转(只要作为您的 Controller 覆盖 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;
}
}
}
关于background - 单点触控。旋转背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8333730/