From 0c91cfc6a25ebe4dce9fa649ca10da02099537a7 Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Thu, 26 Nov 2015 16:59:22 +0200 Subject: [PATCH] Fixed Issue #1160: The image supplied to Label.backgroundImage is rendered upside down --- ui/styling/background.d.ts | 2 +- ui/styling/background.ios.ts | 23 ++++++++++++++++++++++- ui/styling/stylers.ios.ts | 11 ++++------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/ui/styling/background.d.ts b/ui/styling/background.d.ts index ce1ab4ba9..ffd16781f 100644 --- a/ui/styling/background.d.ts +++ b/ui/styling/background.d.ts @@ -44,6 +44,6 @@ declare module "ui/styling/background" { } export module ios { - export function createBackgroundUIColor(view: viewModule.View): any /* UIColor */; + export function createBackgroundUIColor(view: viewModule.View, flip?: boolean): any /* UIColor */; } } diff --git a/ui/styling/background.ios.ts b/ui/styling/background.ios.ts index ab0cfe7a0..17f019b76 100644 --- a/ui/styling/background.ios.ts +++ b/ui/styling/background.ios.ts @@ -5,7 +5,7 @@ import common = require("./background-common"); global.moduleMerge(common, exports); export module ios { - export function createBackgroundUIColor(view: viewModule.View): UIColor { + export function createBackgroundUIColor(view: viewModule.View, flip?: boolean): UIColor { if(!view._nativeView){ return undefined; } @@ -64,9 +64,30 @@ export module ios { img.drawAsPatternInRect(patternRect); } + var bkgImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); + if (flip) { + var flippedImage = _flipImage(bkgImage); + return UIColor.alloc().initWithPatternImage(flippedImage); + } + return UIColor.alloc().initWithPatternImage(bkgImage); } + + // Flipping the default coordinate system + // https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html + function _flipImage(originalImage: UIImage): UIImage { + UIGraphicsBeginImageContextWithOptions(originalImage.size, false, 0.0); + var context = UIGraphicsGetCurrentContext(); + CGContextSaveGState(context); + CGContextTranslateCTM(context, 0.0, originalImage.size.width); + CGContextScaleCTM(context, 1.0, -1.0); + originalImage.drawInRect(CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)) + CGContextRestoreGState(context); + var flippedImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return flippedImage; + } } diff --git a/ui/styling/stylers.ios.ts b/ui/styling/stylers.ios.ts index 88c899d16..e30105666 100644 --- a/ui/styling/stylers.ios.ts +++ b/ui/styling/stylers.ios.ts @@ -323,7 +323,8 @@ export class LabelStyler implements definition.stylers.Styler { private static setBackgroundInternalProperty(view: view.View, newValue: any) { var uiLabel: UILabel = view._nativeView; if (uiLabel && uiLabel.layer) { - var uiColor = background.ios.createBackgroundUIColor(view); + var flipImage = true; + var uiColor = background.ios.createBackgroundUIColor(view, flipImage); var cgColor = uiColor ? uiColor.CGColor : null; uiLabel.layer.backgroundColor = cgColor; } @@ -340,12 +341,8 @@ export class LabelStyler implements definition.stylers.Styler { private static getNativeBackgroundInternalValue(view: view.View): any { var uiLabel: UILabel = view._nativeView; - if (uiLabel && uiLabel.layer) { - var cgColor = uiLabel.layer.backgroundColor; - if (cgColor) { - return UIColor.colorWithCGColor(cgColor); - - } + if (uiLabel && uiLabel.layer && uiLabel.layer.backgroundColor) { + return UIColor.colorWithCGColor(uiLabel.layer.backgroundColor); } return undefined;