Forum Xamarin.iOS

How to render PDF pages as CATiledLayer to the UIImageviews which is a child of UIStackview

Hi,

I am working on implementing the PDFViewer in Xamarin iOS platform.
How to add a CATiledLayer to the Multiple UIImageView which is a child of the UIStackView.
I am trying to achieve paging(Vertical Scrolling) of the PDF document. Using the CATiledLayer i can only add one view in the stackView.

My Design Layout:

UIScrollView-->UIStackView-->UIImageView (Associated with CATiledLayer)

Code:

UIStackView:

public class PdfStackView : UIStackView
{
    public void Initialize()
    {

        PDFDocument.OpenDocument("HTTP Succinctly.pdf","HTTP Succinctly.pdf");
        CGRect rect = PDFDocument.GetPage(1).GetBoxRect(CGPDFBox.Media);
        EmtpyImage = GetEmptyImage(new CGRect(0,0,ConvertToPixel(rect.Width),ConvertToPixel(rect.Height)));                            
        Axis = UILayoutConstraintAxis.Vertical;
        Distribution = UIStackViewDistribution.EqualSpacing;

        //Alignment = UIStackViewAlignment.Leading;
        LayoutMarginsRelativeArrangement = true;
        ContentMode = UIViewContentMode.ScaleAspectFit;
        BackgroundColor = UIColor.Red;
        Spacing = 10;
        CGPDFPage page;
        PdfImageView imageView;
        try
        {
            for (nint i = 0; i < PDFDocument.PageCount; i++)
            {
                page = PDFDocument.GetPage((int)i + 1);
                pageList.Add(i, page);
                count--;
                //if (count < 0)
                //  imageView = new PdfImageView(EmtpyImage,pageList[i].GetBoxRect(CGPDFBox.Media));
                //else
                //{
                    imageView = new PdfImageView(TransformToImage(page, 1f),pageList[i].GetBoxRect(CGPDFBox.Media));
                //}
                RenderedPages.Add(i, imageView);
                imageView.PageIndex = i;
                pageBounds.Add(i, new CGPoint(X,Y));
                Y += ConvertToPixel(page.GetBoxRect(CGPDFBox.Media).Height);
                //pageHeight += page.GetBoxRect(CGPDFBox.Media).Height;

                imageView.parent = this;
                imageView.ContentMode = UIViewContentMode.Redraw;

                //imageView.BackgroundColor = UIColor.Red;
                AddArrangedSubview(imageView);

                TranslatesAutoresizingMaskIntoConstraints = false;
            }
            this.Parent.contentHeight =new CGSize(ConvertToPixel(PDFDocument.GetPage(1).GetBoxRect(CGPDFBox.Media).Height),Y);
            LayoutSubviews();
        }
        catch (Exception e)
        {

        }
    }

}

UIImageView

public class PdfImageView : UIImageView
{
    private nint pageIndex;

    [Export("layerClass")]
    public static Class LayerClass()
    {
        return new Class(typeof(PdfTileViewContent));
    }

    public PdfStackView parent;
    public nint PageIndex
    {
        get
        {
            return pageIndex;
        }
        set
        {
            pageIndex = value;
        }

    }

    public PdfImageView(UIImage image, CGRect frame) : base(frame)
    {

        Image = image;
        AutosizesSubviews = false;
        UserInteractionEnabled = false;
        ClearsContextBeforeDrawing = false;
        ContentStretch = Frame;
        SetNeedsDisplay();
        AutoresizingMask = UIViewAutoresizing.None;
        (Layer as PdfTileViewContent).OnDraw = draw;
    }

    public void draw(CGContext context)
    {

        Image = null;
        context.SetStrokeColor(1.0f, 0, 0, 1.0f);
        context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f);
        using (CGPDFPage pdfPage = parent.document.GetPage(this.pageIndex+1))
        {
            context.TranslateCTM(0, Bounds.Height);
            context.ScaleCTM(1.0f, -1.0f);
            context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, Bounds, 0, true));
            context.SetRenderingIntent(CGColorRenderingIntent.Default);
            context.InterpolationQuality = CGInterpolationQuality.Default;
            context.DrawPDFPage(pdfPage);
        }
    }
}

CATiledLayer

[Register("PdfTileViewContent")]
class PdfTileViewContent : CATiledLayer
{
    #region Data
    public Action<CGContext> OnDraw
    {
        get { return _onDraw; }
        set { _onDraw = value; }
    }
    private Action<CGContext> _onDraw;

    [Export("fadeDuration")]
    public static new double FadeDuration
    {
        get { return 0.001; }
    }
    #endregion

    #region Logic
    public PdfTileViewContent()
    {
        Initialize();
    }

    public PdfTileViewContent(IntPtr handle) : base(handle)
    {
        Initialize();
    }

    public void Initialize()
    {
        LevelsOfDetail = 4;
        LevelsOfDetailBias = 3;
        nfloat wPixels = (UIScreen.MainScreen.Bounds.Width * UIScreen.MainScreen.Scale);
        nfloat hPixels = (UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale);
        nfloat max = (wPixels < hPixels) ? hPixels : wPixels;
        float sizeOfTiles = (max < 512.0f) ? 512.0f : 1024.0f;
        TileSize = new CGSize(sizeOfTiles, sizeOfTiles);
    }

    public override void DrawInContext(CGContext ctx)
    {
        _onDraw(ctx);
    }
    #endregion
}
Sign In or Register to comment.