[C# API] Corrected the scale/translation for iOS

- scale the canvas for smoothing
 - flip the image for CGContext
This commit is contained in:
Matthew Leibowitz 2016-02-06 07:42:32 +02:00
Родитель 927371bda9
Коммит 510ce76591
1 изменённых файлов: 20 добавлений и 22 удалений

Просмотреть файл

@ -1,4 +1,5 @@
using System;
using CoreGraphics;
using UIKit;
using SkiaSharp;
@ -6,6 +7,8 @@ namespace Skia.Forms.Demo.iOS
{
public class NativeSkiaView: UIView
{
const int bitmapInfo = ((int)CGBitmapFlags.ByteOrder32Big) | ((int)CGImageAlphaInfo.PremultipliedLast);
ISkiaViewController skiaView;
public NativeSkiaView (SkiaView skiaView)
@ -13,37 +16,32 @@ namespace Skia.Forms.Demo.iOS
this.skiaView = skiaView;
}
public override void Draw (CoreGraphics.CGRect rect)
public override void Draw (CGRect rect)
{
base.Draw (rect);
// Just not that sharp using the scale pixel scale only
// Going going 2x Pixel Scale
var screenScale = (int)UIScreen.MainScreen.Scale * 2;
var width = (int)Bounds.Width * screenScale;
var height = (int)Bounds.Height * screenScale;
var screenScale = UIScreen.MainScreen.Scale;
var width = (int)(Bounds.Width * screenScale);
var height = (int)(Bounds.Height * screenScale);
IntPtr buff = System.Runtime.InteropServices.Marshal.AllocCoTaskMem (width * height * 4);
try {
using (var surface = SKSurface.Create (width, height, SKColorType.Rgba_8888, SKAlphaType.Premul, buff, width * 4)) {
using (var surface = SKSurface.Create (width, height, SKColorType.N_32, SKAlphaType.Premul, buff, width * 4)) {
var skcanvas = surface.Canvas;
// 2 for one here. Scaling to the pixel size + moving the origin to the top left
skcanvas.Scale (screenScale, -screenScale);
skcanvas.Translate (0, (float)-Frame.Height);
skiaView.SendDraw (skcanvas);
skcanvas.Scale ((float)screenScale, (float)screenScale);
using (new SKAutoCanvasRestore (skcanvas, true)) {
skiaView.SendDraw (skcanvas);
}
}
using (var colorSpace = CoreGraphics.CGColorSpace.CreateDeviceRGB ()) {
int hack = ((int)CoreGraphics.CGBitmapFlags.ByteOrderDefault) | ((int)CoreGraphics.CGImageAlphaInfo.PremultipliedLast);
using (var bContext = new CoreGraphics.CGBitmapContext (buff, width, height, 8, width * 4, colorSpace, (CoreGraphics.CGImageAlphaInfo) hack)) {
using (var image = bContext.ToImage ()) {
using (var context = UIGraphics.GetCurrentContext ()) {
context.DrawImage (Bounds, image);
}
}
}
using (var colorSpace = CGColorSpace.CreateDeviceRGB ())
using (var bContext = new CGBitmapContext (buff, width, height, 8, width * 4, colorSpace, (CGImageAlphaInfo)bitmapInfo))
using (var image = bContext.ToImage ())
using (var context = UIGraphics.GetCurrentContext ()) {
// flip the image for CGContext.DrawImage
context.TranslateCTM (0, Frame.Height);
context.ScaleCTM (1, -1);
context.DrawImage (Bounds, image);
}
} finally {
if (buff != IntPtr.Zero)