Cocoa touch CATiledLayer:为什么添加阴影会使其速度变慢并耗尽内存?

Cocoa touch CATiledLayer:为什么添加阴影会使其速度变慢并耗尽内存?,cocoa-touch,performance,xamarin.ios,calayer,Cocoa Touch,Performance,Xamarin.ios,Calayer,我写了一个小的PDF视图,它逐页显示PDF文件,以减少内存使用。工作正常,但一旦我在查看的页面上添加阴影,它就会耗尽内存并变得非常慢(无法使用)。整个项目的代码如下。如果查看viewWillAspect()方法,您将看到阴影被注释掉。评论一下,整个事情就开始糟糕了。这是什么原因造成的?你甚至可以在模拟器中看到它。只需放大PDF并滚动,它就会被卡住。抛开阴影,一切都会好起来 using System; using MonoTouch.Foundation; using System.IO; usi

我写了一个小的PDF视图,它逐页显示PDF文件,以减少内存使用。工作正常,但一旦我在查看的页面上添加阴影,它就会耗尽内存并变得非常慢(无法使用)。整个项目的代码如下。如果查看viewWillAspect()方法,您将看到阴影被注释掉。评论一下,整个事情就开始糟糕了。这是什么原因造成的?你甚至可以在模拟器中看到它。只需放大PDF并滚动,它就会被卡住。抛开阴影,一切都会好起来

using System;
using MonoTouch.Foundation;
using System.IO;
using MonoTouch.UIKit;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using MonoTouch.CoreGraphics;
using MonoTouch.CoreAnimation;

namespace iOSTest
{
    public class Application
    {
        static void Main ( string[] args )
        {
            UIApplication.Main ( args );
        }
    }

    // The name AppDelegate is referenced in the MainWindow.xib file.
    public partial class AppDelegate : UIApplicationDelegate
    {
        // This method is invoked when the application has loaded its UI and its ready to run
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            NSUrl u = NSUrl.FromString("http://www.tfl.gov.uk/assets/downloads/standard-tube-map.pdf");
            //NSUrl u = NSUrl.FromFilename ("./big.pdf");
            this.o = new AppDelegate.PdfViewController (u);

            this.o.View.Frame = new RectangleF (0, UIApplication.SharedApplication.StatusBarFrame.Height, window.Bounds.Width, window.Bounds.Height - UIApplication.SharedApplication.StatusBarFrame.Height);
            this.o.View.AutoresizingMask = 
                    UIViewAutoresizing.FlexibleWidth
                    | UIViewAutoresizing.FlexibleHeight
                    | UIViewAutoresizing.FlexibleTopMargin
                    | UIViewAutoresizing.FlexibleBottomMargin
                    | UIViewAutoresizing.FlexibleLeftMargin
                    | UIViewAutoresizing.FlexibleRightMargin;

            window.AddSubview (this.o.View);
            //this.oViewMain.BackgroundColor = UIColor.Green;

            //UIApplication.SharedApplication.StatusBarHidden = true;

            window.MakeKeyAndVisible ();
            return true;
        }

        private PdfViewController o;


        /// <summary>
        /// Previews first page of a PDF.
        /// </summary>
        public class PdfViewController : UIViewController
        {
            public PdfViewController(NSUrl oUrl) : base()
            {
                this.oUrl = oUrl;
            }

            private NSUrl oUrl;
            private UIView oContentView;
            private CGPDFDocument oPdfDoc;
            private CGPDFPage oPdfPage;
            private CATiledLayer oTiledLayer;
            private UIScrollView oScrollView;
            private RectangleF oPdfPageRect;

            public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
            {
                return true;
            }

            public override void ViewDidLoad ()
            {
                this.View = new UIView ();
                base.ViewDidLoad ();
            }

            public override void ViewWillAppear (bool animated)
            {
                base.ViewWillAppear (animated);

                // Setup tiled layer.
                this.oTiledLayer = new CATiledLayer ();
                this.oTiledLayer.Delegate = new TiledLayerDelegate (this);
                this.oTiledLayer.TileSize = new SizeF (1024f, 1024f);
                this.oTiledLayer.LevelsOfDetail = 4;
                this.oTiledLayer.LevelsOfDetailBias = 0;
                this.oTiledLayer.BackgroundColor = UIColor.LightGray.CGColor;
                this.oTiledLayer.BorderColor = UIColor.Black.CGColor;
                /*
                this.oTiledLayer.BorderWidth = 1.0f;
                this.oTiledLayer.ShadowColor = UIColor.Black.CGColor;
                this.oTiledLayer.ShadowOffset = new SizeF (20.0f, 20.0f);
                this.oTiledLayer.ShadowOpacity = 0.7f;
                this.oTiledLayer.ShadowRadius = 20.0f;
                */          

                // Setup the view that is hosted by the scroll view.
                this.oContentView = new UIView ();
                this.oContentView.Center = this.View.Center;
                this.oContentView.AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin
                    | UIViewAutoresizing.FlexibleRightMargin
                    | UIViewAutoresizing.FlexibleTopMargin
                    | UIViewAutoresizing.FlexibleBottomMargin;

                this.oContentView.Layer.AddSublayer (this.oTiledLayer);


                // Prepare scroll view.
                this.oScrollView = new UIScrollView (new RectangleF (new PointF (0, 0), this.View.Bounds.Size));
                this.oScrollView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
                this.oScrollView.Delegate = new ScrollViewDelegate (this);
                this.oScrollView.MaximumZoomScale = 4.0f;
                this.oScrollView.BackgroundColor = UIColor.ScrollViewTexturedBackgroundColor;
                // Add the view the scroll view hosts.
                oScrollView.AddSubview (this.oContentView);
                this.View.AddSubview (this.oScrollView);
            }

            public override void ViewDidAppear (bool animated)
            {
                base.ViewDidAppear (animated);

                Console.WriteLine ("Loading PDF: {0}", this.oUrl.ToString ());
                this.oPdfDoc = CGPDFDocument.FromUrl (this.oUrl.ToString ());

                // For demo purposes, show first page only.
                this.oPdfPage = this.oPdfDoc.GetPage (1);

                this.oPdfPageRect = this.oPdfPage.GetBoxRect (CGPDFBox.Media);
                this.oTiledLayer.Frame = this.oPdfPageRect;
                this.oContentView.Frame = this.oPdfPageRect;
                this.oContentView.Center = this.View.Center;
                this.ResizePage ();
            }

            private void ResizePage ()
            {
                // Adjust PDF page to available space. Keep aspect ration.
                this.oScrollView.MinimumZoomScale = 0.001f;
                float fScaleX = this.View.Bounds.Width / this.oPdfPageRect.Width;
                float fScaleY = this.View.Bounds.Height / this.oPdfPageRect.Height;
                float fScale = 0.9f * (fScaleX > fScaleY ? fScaleY : fScaleX);
                this.oScrollView.SetZoomScale (fScale, false);
                this.oScrollView.MinimumZoomScale = fScale;
            }

            public override void ViewDidUnload ()
            {
                base.ViewDidUnload ();
                this.oPdfPage.Dispose ();
                this.oPdfDoc.Dispose ();
                this.oContentView.Dispose ();
                this.oPdfPage = null;
                this.oPdfDoc = null;
                this.oContentView = null;
            }

            public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
            {
                base.DidRotate (fromInterfaceOrientation);

                this.ResizePage ();
            }

            public class TiledLayerDelegate : CALayerDelegate
            {
                public TiledLayerDelegate(PdfViewController oParentController) : base()
                {
                    this.oParentController = oParentController;
                }

                private PdfViewController oParentController;

                public override void DrawLayer (CALayer layer, CGContext context)
                {
                    context.SaveState ();
                    context.SetRGBFillColor (1.0f, 1.0f, 1.0f, 1.0f);
                    context.FillRect (context.GetClipBoundingBox ());
                    context.TranslateCTM (0.0f, layer.Bounds.Size.Height);
                    context.ScaleCTM (1.0f, -1.0f);
                    context.ConcatCTM (this.oParentController.oPdfPage.GetDrawingTransform (CGPDFBox.Media, layer.Bounds, 0, true));
                    context.DrawPDFPage (this.oParentController.oPdfPage);
                    context.RestoreState ();
                }
            }

            public class ScrollViewDelegate : UIScrollViewDelegate
            {
                public ScrollViewDelegate(PdfViewController oParentController) : base()
                {
                    this.oParentController = oParentController;
                }

                private PdfViewController oParentController;

                public override UIView ViewForZoomingInScrollView (UIScrollView scrollView)
                {
                    return this.oParentController.oContentView;
                }

                public override void DidZoom (UIScrollView scrollView)
                {
                    // Override to center PDF page.
                    float fX = scrollView.Bounds.Width > scrollView.ContentSize.Width ? (scrollView.Bounds.Size.Width - scrollView.ContentSize.Width) * 0.5f : 0.0f;
                    float fY = scrollView.Bounds.Height > scrollView.ContentSize.Height ? (scrollView.Bounds.Size.Height - scrollView.ContentSize.Height) * 0.5f : 0.0f;
                    oParentController.oContentView.Center = new PointF (scrollView.ContentSize.Width * 0.5f + fX, scrollView.ContentSize.Height * 0.5f + fY);
                }
            }
        }

    }
}
使用系统;
使用单调的基础;
使用System.IO;
使用MonoTouch.UIKit;
使用系统文本;
使用系统诊断;
使用系统图;
使用MonoTouch.CoreGraphics;
使用MonoTouch.core动画;
名称空间iOSTest
{
公共类应用程序
{
静态void Main(字符串[]参数)
{
UIApplication.Main(args);
}
}
//名称AppDelegate在MainWindow.xib文件中引用。
公共部分类AppDelegate:UIApplicationDelegate
{
//当应用程序加载其UI并准备运行时,将调用此方法
公共覆盖bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
{
NSUrl u=NSUrl.FromString(“http://www.tfl.gov.uk/assets/downloads/standard-tube-map.pdf");
//NSUrl u=NSUrl.FromFilename(“./big.pdf”);
this.o=新的AppDelegate.PdfViewController(u);
this.o.View.Frame=新矩形F(0,UIApplication.SharedApplication.StatusBarFrame.Height,window.Bounds.Width,window.Bounds.Height-UIApplication.SharedApplication.StatusBarFrame.Height);
this.o.View.AutoresizingMask=
UIViewAutoresizing.FlexibleWidth
|UIViewAutoresizing.FlexibleHeight
|UIViewAutoresizing.FlexibleTopMargin
|UIViewAutoresizing.FlexibleBottomMargin
|UIViewAutoresizing.FlexibleLeftMargin
|UIViewAutoResizeing.FlexibleRightMargin;
window.AddSubview(this.o.View);
//this.oViewMain.BackgroundColor=UIColor.Green;
//UIApplication.SharedApplication.StatusBarHidden=true;
window.MakeKeyAndVisible();
返回true;
}
私人PdfViewController o;
/// 
///预览PDF的第一页。
/// 
公共类PdfViewController:UIViewController
{
公共PdfViewController(NSUrl-oUrl):base()
{
this.oUrl=oUrl;
}
私人住宅;
私人UIView-oContentView;
私人CGPDF文件oPdfDoc;
私有CGPDFPage oPdfPage;
私有CATiledLayer或oTiledLayer;
私有UIScrollView和RollView;
私有矩形f optdfpagerect;
公共覆盖布尔值应自动旋转指针面方向(UIInterfaceOrientation到InterfaceOrientation)
{
返回true;
}
公共覆盖无效ViewDidLoad()
{
this.View=newuiview();
base.ViewDidLoad();
}
公共覆盖无效视图将出现(布尔动画)
{
base.view将显示(动画);
//设置平铺层。
this.oTiledLayer=新CATiledLayer();
this.oTiledLayer.Delegate=new TiledLayerDelegate(this);
this.oTiledLayer.TileSize=新的SizeF(1024f,1024f);
this.oTiledLayer.LevelsOfDetail=4;
this.oTiledLayer.LevelsOfDetailBias=0;
this.oTiledLayer.BackgroundColor=UIColor.LightGray.CGColor;
this.oTiledLayer.BorderColor=UIColor.Black.CGColor;
/*
this.oTiledLayer.BorderWidth=1.0f;
this.oTiledLayer.ShadowColor=UIColor.Black.CGColor;
this.oTiledLayer.ShadowOffset=新尺寸f(20.0f,20.0f);
this.oTiledLayer.ShadowOpacity=0.7f;
this.oTiledLayer.ShadowRadius=20.0f;
*/          
//设置由滚动视图承载的视图。
this.oContentView=newuiview();
this.oContentView.Center=this.View.Center;
this.oContentView.AutoresizingMask=ui视图autoresizing.FlexibleLeftMargin
|UIViewAutoresizing.FlexibleRightMargin
|UIViewAutoresizing.FlexibleTopMargin
|UIViewAutoresizing.FlexibleBottomMargin;
this.oContentView.Layer.AddSublayer(this.oTiledLayer);
//准备滚动视图。
this.oScrollView=newUIScrollView(新矩形F(新点F(0,0),this.View.Bounds.Size));
this.oScrollView.AutoresizingMask=UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
this.oScrollView.Delegate=新的ScrollViewDelegate(this);
this.oScrollView.MaximumZoomScale=4.0f;
this.oScrollView.BackgroundColor=UIColor.ScrollViewTexturedBackgroundColor;
//将视图添加到滚动视图主机。
oScrollView.AddSubview(this.oContentView);
this.View.AddSubview(this.oScrollView);
}
公共覆盖无效视图显示(布尔动画)
{
base.viewdid显示(动画);
Console.WriteLine(“加载PDF:{0}”,this.oUrl.ToString());
this.oPdfDoc=CGPDFDocument.FromUrl(this.oUrl.ToString());
//为了演示目的
[UIBezierPath pathWithRect:pdfView.bounds].CGPath