How to get UIImage from PDFDraw on Xamarin?

14 views
Skip to first unread message

Ryan

unread,
Feb 27, 2017, 1:39:39 PM2/27/17
to PDFTron PDFNet SDK
Question:

I am on Xamarin, and need to get a UIImage object from PDFDraw, how do I that?

Answer:

private UIImage ImageFromRawBGRAData(NSData data, int width, int height, bool isRetina)
{
   
using (CGDataProvider provider = new CGDataProvider(data))
   
{
       
int bitsPerComponent = 8;
       
int bitsPerPixel = 4 * bitsPerComponent;
       
int bytesPerRow = 4 * width;
       
using (var colorSpace = CGColorSpace.CreateDeviceRGB())
       
{
           
const CGBitmapFlags bitmapInfo = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
           
const CGColorRenderingIntent renderingIntent = CGColorRenderingIntent.Default;
           
using (var image = new CGImage(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
                colorSpace
, bitmapInfo, provider, null, false, renderingIntent))
           
{
                nfloat screenScale
= 1;
               
if (isRetina && UIScreen.MainScreen.Scale > 1)
               
{
                    screenScale
= UIScreen.MainScreen.Scale;
               
}

               
var myImage = new UIImage(image, screenScale, UIImageOrientation.Up);
               
return myImage;
           
}
       
}
   
}
}

Reply all
Reply to author
Forward
0 new messages