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 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); global.moduleMerge(common, exports);
export module ios { export module ios {
export function createBackgroundUIColor(view: viewModule.View): UIColor { export function createBackgroundUIColor(view: viewModule.View, flip?: boolean): UIColor {
if(!view._nativeView){ if(!view._nativeView){
return undefined; return undefined;
} }
@ -64,9 +64,30 @@ export module ios {
img.drawAsPatternInRect(patternRect); img.drawAsPatternInRect(patternRect);
} }
var bkgImage = UIGraphicsGetImageFromCurrentImageContext(); var bkgImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); UIGraphicsEndImageContext();
if (flip) {
var flippedImage = _flipImage(bkgImage);
return UIColor.alloc().initWithPatternImage(flippedImage);
}
return UIColor.alloc().initWithPatternImage(bkgImage); 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) { private static setBackgroundInternalProperty(view: view.View, newValue: any) {
var uiLabel: UILabel = <UILabel>view._nativeView; var uiLabel: UILabel = <UILabel>view._nativeView;
if (uiLabel && uiLabel.layer) { 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; var cgColor = uiColor ? uiColor.CGColor : null;
uiLabel.layer.backgroundColor = cgColor; uiLabel.layer.backgroundColor = cgColor;
} }
@ -340,12 +341,8 @@ export class LabelStyler implements definition.stylers.Styler {
private static getNativeBackgroundInternalValue(view: view.View): any { private static getNativeBackgroundInternalValue(view: view.View): any {
var uiLabel: UILabel = <UILabel>view._nativeView; var uiLabel: UILabel = <UILabel>view._nativeView;
if (uiLabel && uiLabel.layer) { if (uiLabel && uiLabel.layer && uiLabel.layer.backgroundColor) {
var cgColor = uiLabel.layer.backgroundColor; return UIColor.colorWithCGColor(uiLabel.layer.backgroundColor);
if (cgColor) {
return UIColor.colorWithCGColor(cgColor);
}
} }
return undefined; return undefined;