react-native-image-zoom icon indicating copy to clipboard operation
react-native-image-zoom copied to clipboard

Built-in image size management feasible?

Open hardcodet opened this issue 7 years ago • 6 comments

Wouldn't it make sense to provide the component with the capabilities to just show an image in contain mode without having to specify its size, and have it initially just match the container's size? That would probably the case in 95+ % of all cases, not?

hardcodet avatar Jan 16 '18 08:01 hardcodet

Image component from RN needs WxH, otherwise, it will not show the image.

dhcmega avatar Jan 17 '18 13:01 dhcmega

Hi, Can you give a chance to remove overflow: Hidden, because I need to see full image, otherwise it is showing cropped image https://github.com/ascoders/react-native-image-zoom/blob/master/src/image-zoom/image-zoom.style.ts#L6

dhanababum avatar May 14 '18 17:05 dhanababum

@dhanababum I suggest you create a Pull Request if you want that change implemented; @ascoders is welcoming of outside contributions 👍

brycehanscomb avatar Oct 21 '18 02:10 brycehanscomb

My workaround for this was to load the image first inside the render function, making sure the Image component has an onLayout handler passed in its props, then once the dimensions are known (using onLayout's event.nativeEvent.layout.width), insert the ImageZoom component into the render function using the newly discovered dimensions.

darriuk avatar Feb 01 '19 18:02 darriuk

@darriuk your solution sound really good. Can you paste an example or it will be too much code? Thanks.

dhcmega avatar Feb 01 '19 19:02 dhcmega

Yeah no probs. My code works out the image aspect ratio from the width and height, so that the image always fill the entire screen width and has a dynamic height depending on its aspect ratio.

constructor(props){
    super(props);
    this.state={
        imageHeight:0
    };
}

/**
 * Handle the initial Image onLayout event
 * @param {SyntheticEvent} event
 */
handleOnLayout = (event) =>{
    let w = event.nativeEvent.layout.width;
    let h = event.nativeEvent.layout.height;
    let ratio = w/h;
    this.setState({
        imageHeight: Dimensions.get('window').width / ratio
    })
}

render() {
    let file = require("path/to/image/file");
    let screenWidth = Dimensions.get('window').width;
    let screenHeight = Dimensions.get('window').height;
    return (
        <View 
            style={{
                width:'100%',
                height:'100%'
            }}
        >
            
            {this.state.imageHeight === 0 ? 
                <Image 
                    style={{opacity:0}}
                    onLayout={this.handleOnLayout}
                    source={file}
                />
            :
                <ImageZoom 
                    cropWidth={screenWidth}
                    cropHeight={screenHeight}
                    imageWidth={screenWidth}
                    imageHeight={this.state.imageHeight}
                >
                    <Image 
                        style={{
                            width:screenWidth,
                            height:this.state.imageHeight
                        }}
                        source={file}
                    />
                </ImageZoom>
            }
        </View>
    );
}

darriuk avatar Feb 01 '19 22:02 darriuk