Skip to content

Commit 4e678a0

Browse files
committed
Fix: Images have margin when added to Layout
Resolves NativeScript#1425
1 parent 423066a commit 4e678a0

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ tns-core-modules.es6.d.ts
4242
tests/platforms/
4343
tests/lib/
4444
*.log
45+
46+
.DS_Store

tests/app/ui/image/700x50.png

808 Bytes
Loading

tests/app/ui/image/image-tests.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {StackLayout} from "ui/layouts/stack-layout";
33
import {GridLayout} from "ui/layouts/grid-layout";
44
import {isIOS} from "platform";
55
import {PropertyChangeData} from "data/observable";
6+
import utils = require("utils/utils");
67

78
// import {target} from "../../TKUnit";
89

@@ -334,3 +335,23 @@ export var test_SettingImageSourceWhenSizedToContentShouldInvalidate = ios(() =>
334335
TKUnit.assertTrue(called, "image.requestLayout should be called.");
335336
});
336337

338+
export var test_DimensionsAreRoundedAfterScale = function() {
339+
let host = new StackLayout();
340+
let image = new Image();
341+
image.src = "~/ui/image/700x50.png";
342+
343+
let density = utils.layout.getDisplayDensity();
344+
let limit = 414;
345+
host.width = limit / density;
346+
host.height = limit / density;
347+
host.addChild(image);
348+
let mainPage = helper.getCurrentPage();
349+
mainPage.content = host;
350+
TKUnit.waitUntilReady(() => host.isLoaded);
351+
TKUnit.waitUntilReady(() => image.isLayoutValid);
352+
353+
let scale = limit / 700;
354+
let expectedHeight = Math.round(50 * scale);
355+
TKUnit.assertEqual(image.getMeasuredWidth(), limit, "Actual width is different from expected width.");
356+
TKUnit.assertEqual(image.getMeasuredHeight(), expectedHeight, "Actual height is different from expected height.");
357+
};

tns-core-modules/ui/image/image.ios.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ export class Image extends imageCommon.Image {
8383

8484
if (nativeWidth !== 0 && nativeHeight !== 0 && (finiteWidth || finiteHeight)) {
8585
var scale = Image.computeScaleFactor(width, height, finiteWidth, finiteHeight, nativeWidth, nativeHeight, this.stretch);
86-
var resultW = Math.floor(nativeWidth * scale.width);
87-
var resultH = Math.floor(nativeHeight * scale.height);
86+
var resultW = Math.round(nativeWidth * scale.width);
87+
var resultH = Math.round(nativeHeight * scale.height);
8888

8989
measureWidth = finiteWidth ? Math.min(resultW, width) : resultW;
9090
measureHeight = finiteHeight ? Math.min(resultH, height) : resultH;

0 commit comments

Comments
 (0)