{"id":44458,"date":"2017-01-05T11:12:26","date_gmt":"2017-01-05T05:42:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=44458"},"modified":"2018-02-26T12:21:21","modified_gmt":"2018-02-26T06:51:21","slug":"simplistic-fullscreen-image-viewer-react-native","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/simplistic-fullscreen-image-viewer-react-native\/","title":{"rendered":"Simplistic Fullscreen Image Viewer: React Native"},"content":{"rendered":"<p>If you are working with the latest version of <a title=\"React Native Development Company\" href=\"http:\/\/www.tothenew.com\/react-native-development-services\">React Native<\/a> like <code>0.35<\/code>\u00a0or above, then there are lot of modules available to show an image in full screen mode. Now the twist here is when you are working with the older version of React Native. In this blog, I wanted to share how I implemented this use case\u00a0on the application on React Native\u00a0 <code>0.21\u00a0<\/code>and React\u00a0<code>0.14.8<\/code> versions\u00a0respectively.<\/p>\n<p>One of the biggest challenge with the older versions are that it does not support all the node modules. However if they support too, you still can&#8217;t upgrade the version of the modules that you are using for latest feature support. It is very important to keep in mind the compatibility issues and also ensure that it\u00a0should work for both <a title=\"android application development services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\">Android <\/a>and <a title=\"iOS application development services\" href=\"http:\/\/www.tothenew.com\/mobile-ios-application-development-services\">iOS<\/a>.<\/p>\n<p>You might be thinking that why am I not upgrading my React Native version to the latest one? But that&#8217;s not easy too! \u00a0When I tried upgrading from the existing React Native version to latest one, I noticed that few modules on my application were\u00a0breaking. Also,\u00a0\u00a0when your application is already deployed in production you really need to prioritize between investing more time and effort in upgrading the application or releasing the features of your application which are more useful to your customers.<\/p>\n<p>So, in this blog,\u00a0I will be helping out my developer buddies who due to few constraints cannot use jazzy npm modules like https:\/\/github.com\/alwx\/react-native-photo-view and want to show images in their application beautifully in full screen mode something like a lightbox.<\/p>\n<p><strong>Note: I have also opened an issue on github repo about the build errors I got when I tried using this module. You can follow it here :<\/strong> https:\/\/github.com\/alwx\/react-native-photo-view\/issues\/23 . Having said this, I believe this module can be a good option if you are working with latest versions of React and React-Native.<\/p>\n<p>We will be using <code>react-native-overlay<\/code> (https:\/\/github.com\/brentvatne\/react-native-overlay) module and CSS for implementing this. Now don&#8217;t get startled by <strong>readme.md<\/strong> of this module which says <em>&#8220;Should you use this? Ideally, no. This should probably only be used as a last resort. You can usually accomplish what you need to by just absolute positioning an view at the bottom of your root component.&#8221;<\/em> Remember our React Native version is old and <strong>zIndex<\/strong> is supported on version <code>0.29<\/code> and above.<\/p>\n<p>First, lets go through the code snippet below (Assuming you are familiar with development in React Native) :<\/p>\n<p>[js]<br \/>\nimport React, {<br \/>\n  Component,<br \/>\n  View,<br \/>\n  Image,<br \/>\n  TouchableOpacity,<br \/>\n  TouchableHighlight,<br \/>\n} from &#8216;react-native&#8217;;<\/p>\n<p>import Icon from  &#8216;react-native-vector-icons\/Ionicons&#8217;;<br \/>\nconst Overlay = require(&#8216;react-native-overlay&#8217;); <\/p>\n<p>export default class ImageView extends Component {<\/p>\n<p>state = {<br \/>\n  imageUrl : &#8216;http:\/\/www.tothenew.com\/themes\/ttnd\/images\/logo.png&#8217;<br \/>\n}<\/p>\n<p>render() {<br \/>\n\treturn &amp;lt;View style={{flex: 1}}&amp;gt;<br \/>\n     &amp;lt;View&amp;gt;<br \/>\n          &amp;lt;TouchableOpacity onPress={()=&amp;gt;{this.setState({isVisible: true})}}<br \/>\n                style={{flex: 1}}&amp;gt;<br \/>\n                &amp;lt;Image resizeMode=&amp;quot;cover&amp;quot;<br \/>\n                  source={{uri: this.state.image}}&amp;gt;<br \/>\n                &amp;lt;\/Image&amp;gt;<br \/>\n          &amp;lt;\/TouchableOpacity&amp;gt;<br \/>\n     &amp;lt;\/View&amp;gt;<br \/>\n     &amp;lt;Overlay isVisible={this.state.isVisible}&amp;gt;<br \/>\n        &amp;lt;View style={styles.fullscreenImage}&amp;gt;<br \/>\n          &amp;lt;Image resizeMode=&amp;quot;contain&amp;quot;<br \/>\n            source={{uri: this.state.imageUrl}} style={{flex: 1}}&amp;gt;<br \/>\n          &amp;lt;\/Image&amp;gt;<br \/>\n          &amp;lt;TouchableHighlight<br \/>\n            style={styles.overlayCancel}<br \/>\n            onPress={()=&amp;gt;{this.setState({isVisible: false})}}&amp;gt;<br \/>\n            &amp;lt;Icon name=&amp;quot;close&amp;quot;<br \/>\n              style={styles.cancelIcon} size={28} \/&amp;gt;<br \/>\n          &amp;lt;\/TouchableHighlight&amp;gt;<br \/>\n        &amp;lt;\/View&amp;gt;<br \/>\n      &amp;lt;\/Overlay&amp;gt;<br \/>\n     &amp;lt;\/View&amp;gt;<br \/>\n   }<br \/>\n}<\/p>\n<p>const styles = StyleSheet.create({<br \/>\n\toverlayCancel: {<br \/>\n    padding: 20,<br \/>\n    position: &#8216;absolute&#8217;,<br \/>\n    right: 10,<br \/>\n    top: 0,<br \/>\n  },<br \/>\n   cancelIcon: {<br \/>\n    color: &#8216;white&#8217;,<br \/>\n  },<br \/>\n});<br \/>\n[\/js]<\/p>\n<p>To use <strong>react-native-overlay<\/strong> module, run <code>npm install react-native-overlay --save<\/code> in the terminal from your project directory and require it in the file where you are showing your image. As you can see in above lines of code, we have added an <strong>onPress<\/strong> event on actual image and on clicking this we will show the <strong>Overlay<\/strong> tag by setting <strong>isVisible<\/strong> field to true. Now it is the duty of Overlay module to show the image in fullscreen mode. We have also added a cross button in the top right corner to close the fullscreen image and resume to initial image view. The styling may vary according to UI of your application and you can anytime customize that too.<\/p>\n<p>Here, you will notice that I have used another module <code>react-native-vector-icons<\/code> for showing a cross icon. You can explore more options for icons here : https:\/\/github.com\/oblador\/react-native-vector-icons<br \/>\nSo on clicking the image, the final fullscreen view will look something like this:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-44706\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/01\/fullscreen-image.png\" alt=\"fullscreen-image\" width=\"373\" height=\"680\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/01\/fullscreen-image.png 373w, \/blog\/wp-ttn-blog\/uploads\/2017\/01\/fullscreen-image-164x300.png 164w\" sizes=\"(max-width: 373px) 100vw, 373px\" \/><\/p>\n<p>The best part about this solution is that it works well on both Android and iOS.<\/p>\n<p>I hope this will be of some help. Keep watching this space for more real life challenges and their solutions in React Native world. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog I will be helping out my developer buddies who due to few constraints cannot use jazzy npm modules  to show images in their application beautifully in fullscreen mode something like a lightbox.  We will be using react-native-overlay (https:\/\/github.com\/brentvatne\/react-native-overlay) module and css for implementing this.<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":30},"categories":[518,1400,1772,1],"tags":[4845,4848,4064,3505],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/44458"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=44458"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/44458\/revisions"}],"predecessor-version":[{"id":52760,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/44458\/revisions\/52760"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=44458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=44458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=44458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}