Skip to content

Multi resolution images #1456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-env jasmine, jest */
/* eslint-disable react/jsx-no-bind */

import * as AssetRegistry from '../../../modules/AssetRegistry';
import Image from '../';
import ImageLoader from '../../../modules/ImageLoader';
import ImageUriCache from '../ImageUriCache';
import PixelRatio from '../../PixelRatio';
import React from 'react';
import { shallow } from 'enzyme';
import StyleSheet from '../../StyleSheet';
Expand Down Expand Up @@ -225,6 +227,24 @@ describe('components/Image', () => {
loadCallback();
expect(component.find('img').prop('src')).toBe(uri);
});

test('it correctly selects the source scale', () => {
AssetRegistry.getAssetByID = jest.fn(() => ({
httpServerLocation: 'static',
name: 'img',
scales: [1, 2, 3],
type: 'png'
}));
PixelRatio.get = jest.fn(() => 2.2);

const component = shallow(<Image source={1} />);
expect(
component
.render()
.find('img')
.attr('src')
).toBe('static/[email protected]');
});
});

describe('prop "style"', () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/react-native-web/src/exports/Image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ImageResizeMode from './ImageResizeMode';
import ImageSourcePropType from './ImageSourcePropType';
import ImageStylePropTypes from './ImageStylePropTypes';
import ImageUriCache from './ImageUriCache';
import PixelRatio from '../PixelRatio';
import StyleSheet from '../StyleSheet';
import StyleSheetPropType from '../../modules/StyleSheetPropType';
import View from '../View';
Expand Down Expand Up @@ -53,7 +54,14 @@ const resolveAssetUri = source => {
if (typeof source === 'number') {
// get the URI from the packager
const asset = getAssetByID(source);
const scale = asset.scales[0];
let scale = asset.scales[0];
if (asset.scales.length > 1) {
const preferredScale = PixelRatio.get();
// Get the scale which is closest to the preferred scale
scale = asset.scales.reduce((prev, curr) =>
Math.abs(curr - preferredScale) < Math.abs(prev - preferredScale) ? curr : prev
);
}
const scaleSuffix = scale !== 1 ? `@${scale}x` : '';
uri = asset ? `${asset.httpServerLocation}/${asset.name}${scaleSuffix}.${asset.type}` : '';
} else if (typeof source === 'string') {
Expand Down