Fixed Issue #1160: The image supplied to Label.backgroundImage is rendered upside down

This commit is contained in:
Rossen Hristov
2015-11-26 16:59:22 +02:00
parent c873a7d4de
commit 0c91cfc6a2
3 changed files with 27 additions and 9 deletions

View File

@ -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 */;
}
}

View File

@ -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;
}
}

View File

@ -323,7 +323,8 @@ export class LabelStyler implements definition.stylers.Styler {
private static setBackgroundInternalProperty(view: view.View, newValue: any) {
var uiLabel: UILabel = <UILabel>view._nativeView;
if (uiLabel && uiLabel.layer) {
var uiColor = <UIColor>background.ios.createBackgroundUIColor(view);
var flipImage = true;
var uiColor = <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 = <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;