Skip to content

Commit 62c1d20

Browse files
authored
Merge pull request NativeScript#2306 from NativeScript/issue-1425
Fix: Images have margin when added to Layout
2 parents 9c60210 + be62b27 commit 62c1d20

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-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: 23 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,25 @@ 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+
let imageWidth = 700;
343+
let imageHeight = 50;
344+
345+
let density = utils.layout.getDisplayDensity();
346+
let hostWidth = 320;
347+
host.width = hostWidth / density;
348+
host.height = hostWidth / density;
349+
host.addChild(image);
350+
let mainPage = helper.getCurrentPage();
351+
mainPage.content = host;
352+
TKUnit.waitUntilReady(() => host.isLoaded);
353+
TKUnit.waitUntilReady(() => image.isLayoutValid);
354+
355+
let scale = hostWidth / imageWidth;
356+
let expectedHeight = Math.round(imageHeight * scale);
357+
TKUnit.assertEqual(image.getMeasuredWidth(), hostWidth, "Actual width is different from expected width.");
358+
TKUnit.assertEqual(image.getMeasuredHeight(), expectedHeight, "Actual height is different from expected height.");
359+
};

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)