Can't get the size of a local image - react-native

I'm trying to get the height of a local image to scale it on React-Native. I tried the following code but I get Failed to get size for image: ../../images/body.png error.
scaleImg(h) {
Image.getSize('../../images/body.png', (width, height) => {
const scaleFactor = height / h
const imageWidth = width / scaleFactor
this.setState({imgWidth: imageWidth, imgHeight: h})
})
}

I'm late to the party, but there's still no answer. For local assets you gotta use Image.resolveAssetSource(require('./path')); instead.

As per the documentation, you can't use Image.getSize on static image resources, which is what your path seems to link to.
static getSize(uri: string, success: function, failure?: function):
Retrieve the width and height (in pixels) of an image prior to
displaying it. This method can fail if the image cannot be found, or
fails to download.
In order to retrieve the image dimensions, the image may first need to
be loaded or downloaded, after which it will be cached. This means
that in principle you could use this method to preload images, however
it is not optimized for that purpose, and may in future be implemented
in a way that does not fully load/download the image data. A proper,
supported way to preload images will be provided as a separate API.
Does not work for static image resources.

Related

React Native view scaling

So I'm developing a cross platform React Native app, the app is using allot of images as buttons as per design requirements that need to be given an initial height and width so that their aspect ratios are correct. From there I've built components that use these image buttons and then placed those components on the main screen. I can get things to look perfect on one screen by using tops and lefts/ rights to get the components positioned according to the design requirements that I've been given.
The problem I'm running into is now scaling this main screen for different screen sizes. I'm basically scaling the x and y via the transform property on the parent most view as such. transform: [{ scaleX: .8 }, { scaleY: .8 }] After writing a scaling function that accounts for a base height and current height this approach works for the actual size of things but my positioning is all screwy.
I know I'm going about this wrong and am starting to think that i need to rethink my approach but am stumped on how to get these components positioned correctly on each screen without having to hard code it.
Is there any way to position a view using tops and lefts/rights, lock that in place, then scale it more like an image?
First of all, try using flex as far as you can. Then when you need extra scaling for inner parts for example, you can use scale functions. I have been using a scale function based on the screen size and the pixel density, and works almost flawless so far.
import { Dimensions } from "react-native";
const { width, height } = Dimensions.get("window");
//Guideline sizes are based on standard ~5" screen mobile device
const guidelineBaseWidth = 350;
const guidelineBaseHeight = 680;
const screenSize = Math.sqrt(width * height) / 100;
const scale = size => (width / guidelineBaseWidth) * size;
const verticalScale = size => (height / guidelineBaseHeight) * size;
const moderateScale = (size, factor = 0.5) =>
size + (scale(size) - size) * factor;
export { scale, verticalScale, moderateScale, screenSize };
Then you can import these and use in your components. There are different types of scales, you can try and see the best one for your components.Hope that helps.
I ended up going through each view and converting everything that was using a hard coded height and width pixel to setting the width and then using the property aspectRatio and giving that the hard coded height and widths. That along with implementing a scaling function that gave me a fraction of the largest view, so say .9, and then scaling the main view using transform. People arent kidding when they say this responsive ui stuff is tough.
2022 update -
I resolved this problem on my next app by using flex everywhere & a function called rem that I use everywhere that needs a fixed pixel count. With this I can set the width on an image and define an aspect ratio based on the images original dimensions and get an image that scales to the screen size, it's been super reliable.
static width = Dimensions.get("window").width;
static height = Dimensions.get("window").height;
static orientation = 'PORTRAIT';
static maxWidth = 428;
static rem = size => {
let divisor = window.lockedToPortrait || Styles.orientation === 'PORTRAIT' ? Styles.width : Styles.height;
return Math.floor(size * (divisor / Styles.maxWidth))
};
The maxWidth is a predefined value from the largest device I could find to simulate which was probably an iPhone max.

How do I get phantomJS to generate pdf that is just a single page that contains my page?

I am trying to create a pdf from a webpage using phantomJS. I want the pdf to just be a single page long that is the same height as my webpage. If I don't set paperSize or viewportSize at all then it creates a page that is almost the right height, it is just a little short by about 100px. If I detect page height and use that to set viewportSize I have the exact same problem, it's short by about 100px. Is there a way to get phantomJS to just create a pdf based on the webpage's size? I want it to be exact because the page is a dark colour and it looks really bad to have a big white block at the end of the page (which is what is there if I add a constant to viewport height to make it fit everything in one page).
My code to find height(Note: the height is the same whether I use body or the classname below to find it):
var size = page.evaluate(function() {
var height = $('.export-athlete-detail-report').outerHeight(true);
return {width: 700, height:height};
});
page.paperSize = size;
page.render(reportFile);

How to change the width of a movieclip in flash html5 canvas?

I was created a new project using flash cc html5 canvas and created a movieclip with 200px width and 200px height. I can able to get the properties using this.mc.nominalBounds;. And I tried this.mc.getBounds ();
But it returns null. setBounds also not seems to work.
The setBounds method is for putting a custom bounds on something, or defining it when it can not be determined (like using the Graphics classes). Fortunately, all symbols from Flash CC come with nominalBounds, which is the combined, untransformed size of the symbol.
Using that, you can set the scaleX and scaleY properties to size up and down your content. If you want it to be a specific width, just use the nominalBounds to determine a new scale.
For example, if the clip is 200x200, and you want it to be 400x400, then:
var scale = newWidth / nominalBounds.width;
clip.scaleX = clip.scaleY = scale;
Hope that helps!

ImageBuilder.Current.DecodeStream() doesn't resize

I'm passing a memory stream to the ImageBuilder.Current.DecodeStream() method, with some resize settings, but it's not resizing the resulting bitmap.
var bitmapData = Convert.FromBase64String(base64Image.FixBase64ForImage());
var streamBitmap = new MemoryStream(bitmapData);
var bitImage = ImageBuilder.Current.DecodeStream(streamBitmap, settings, "");
FixBase64ForImage() is just an extension method I wrote to remove the data:image/jpg;;base64, from the submitted base64 encoded photo.
I pass a 640x640 square photo to the ImageBuilder.Current.DecodeStream() method, and pass it settings that looks like this
var settings = new ResizeSettings(string.Format("maxwidth={0}&maxheight={1}&format={2}&crop={3}&quality={4}", maxwidth, maxheight,format, crop, quality));
No matter what max width/height I pass it, it always returns the full 640x640 photo (and seems like there is no quality compression as well).
This leads me to believe the resize settings are simply ignored. Is there another way to accomplish the same thing, or am I doing this wrong?
ImageBuilder.Current.DecodeStream does not do any image processing; it is only for decoding an image stream into an image bitmap. It is called by .LoadImage(), among others.
Use the .Build() overloads to perform image processing.

How to resize image to a fixed height and width without losing aspect ratio in winjs?

I have an image area in my application with width:530px and height:510px. I want to place images in that area but the images comes in different different sizes. how to crop or scale the image without losing aspect ratio to fill that area. Is there any native methods available in winjs?
You have a few options for that.
One is to use the ViewBox control that WinJS provides you. The ViewBox can only have a single child (so you would have your tag as its only child perhaps or a div that contains your img) and it will scale that child (using CSS transforms) up to fit into its container (without changing the aspect ratio). It's pretty slick.
Another option is to set the image as the CSS background-image property of a container (such as a div) and set the background-size property to contain. This will stretch the image up to the size of the container.
A final option that you have to resort to if your case is a bit special is not such a bad option after all. In the updateLayout method of your page, you can refer to the element and explicitly set its CSS properties to fit. At that point you'll know all about the layout and can easily do the math to figure out what size image should be. Here's some code from one of my projects where I do this. Notice that I'm comparing the aspect ratio of the screen and the image to determine whether I should size to the width or the height. Unlike your situation (I'm guessing), my code makes sure the image fills the screen and excess is clipped. I'm guessing you want your img to be contained.
function setImagePosition() {
var img = q(".viewCamera #viewport img");
if (outerWidth/outerHeight > img.naturalWidth/img.naturalHeight) {
img.style.width = format("{0}px", outerWidth);
img.style.height = "";
img.style.top = format("{0}px", (outerHeight - img.clientHeight) / 2);
img.style.left = "0px";
} else {
img.style.width = "";
img.style.height = format("{0}px", outerHeight);
img.style.top = "0px";
img.style.left = format("{0}px", (outerWidth - img.clientWidth) / 2);
}
}
Hope that helps!
Are you referring to scaling HTML images? If so, you can set either one, width or height. Whichever you set, the other will scale and keep image aspect ratio.