Giter Site home page Giter Site logo

Comments (35)

sanealytics avatar sanealytics commented on July 18, 2024 22

Here is the code that works in my specific use case

            <View>
              { !this.state.isImgLoaded && (
                <FastImage
                  style={{
                    width: 400,
                    height: 450,
                    position: 'absolute',
                  }}  
                  source={{uri: photo.uri}}
                  resizeMode={FastImage.resizeMode.contain}
                />  
              ) } 
              <FastImage
                style={{
                  width: 400,
                  height: 450,
                }}  
                source={{uri: photo.images[this.state.count % photo.images.length]}}
                onLoadEnd={this.handleImgLoaded}
                resizeMode={FastImage.resizeMode.contain}
              />  
            </View>

elsewhere -

  handleImgLoaded = () =>
    this.setState({ isImgLoaded: true })

Great package btw! One of the better written packages out there.

from react-native-fast-image.

DylanVann avatar DylanVann commented on July 18, 2024 20

Yeah I was thinking of including that but cut it to get the lib released. I'll take a look at how that could be done now.

from react-native-fast-image.

timothystewart6 avatar timothystewart6 commented on July 18, 2024 15

** Edit **
Now that I've read the request more this won't address the placeholder feature but it is a workaround for a fallback so I'll leave the comment. Hope this helps.

I've managed to do it like this:

      <FastImage          
          source={{
            uri: `https://www.example.com/example.png`,
          }}
          fallback
          defaultSource={require(../images/fallback.png))}
        />

I don't think defaultSource works with <FastImage /> but it does with <Image /> so I set fallback so that on an error, it falls back to <Image /> which supports defaultSource

from react-native-fast-image.

staffannase avatar staffannase commented on July 18, 2024 7

Not sure if other people have this issue but I tend to get a blink when using the wrapper on pages where the image is visible on page load.

from react-native-fast-image.

sebqq avatar sebqq commented on July 18, 2024 7

I've prepared custom wrapper using hooks.

import React, { useState, useMemo, memo } from "react";
import PropTypes from "prop-types";
import { View, StyleSheet } from "react-native";
import CachedImage from "react-native-fast-image";

const FastImage = memo(
  ({
    renderPlaceholder,
    renderErrorImage,
    onError,
    onLoad,
    imageStyle,
    ...otherProps
  }) => {
    const [isLoading, setLoading] = useState(true);
    const [isErrored, setIsErrored] = useState(false);

    const CachedImageMemoized = useMemo(() => {
      return (
        <CachedImage
          {...otherProps}
          style={[imageStyle, styles.image]}
          onError={() => {
            setLoading(false);
            setIsErrored(true);
            onError && onError();
          }}
          onLoad={e => {
            setLoading(false);
            onLoad && onLoad(e);
          }}
        />
      );
    }, [onError, onLoad, imageStyle, otherProps]);

    return (
      <View style={imageStyle}>
        {CachedImageMemoized}
        {isLoading && renderPlaceholder()}
        {isErrored && renderErrorImage()}
      </View>
    );
  }
);

FastImage.priority = CachedImage.priority;
FastImage.resizeMode = CachedImage.resizeMode;

FastImage.propTypes = {
  renderPlaceholder: PropTypes.func,
  renderErrorImage: PropTypes.func,
  onError: PropTypes.func,
  onLoad: PropTypes.func,
  imageStyle: PropTypes.object.isRequired
  // Your imageStyle can look like:
  // imageStyle = {width: XYZ, height: XYZ}
};

const styles = StyleSheet.create({
  image: { position: "absolute", zIndex: -1 }
});

export default FastImage;

I've tried solutions above but I noticed some perforamnce issues. This works for me really well. And it will work for you also if you will memoize props in parent component.

Using this solution you are free to use any react component as Placeholder image and any react component as Error image too.

from react-native-fast-image.

codingwaysarg avatar codingwaysarg commented on July 18, 2024 6

Hi! I solved it by using a wrapper component. This is my wrapper component.

PlaceHolderFastImage

from react-native-fast-image.

Angelk90 avatar Angelk90 commented on July 18, 2024 4

@DylanVann : Are there any news?
I saw this: #109 (comment)

from react-native-fast-image.

danielgindi avatar danielgindi commented on July 18, 2024 3

Solved in both Android and iOS in #550

from react-native-fast-image.

AlirezaAkbarix avatar AlirezaAkbarix commented on July 18, 2024 2

@Angelk90! You set placeholder prop as default source, then use onLoadEnd method to replace that placeholder with new grabbed image (this method should be changed to return image).

from react-native-fast-image.

EgidioCaprino avatar EgidioCaprino commented on July 18, 2024 2

This PR adds placeholder prop #409

from react-native-fast-image.

Meandmybadself avatar Meandmybadself commented on July 18, 2024 1

@AlirezaAkbarix – Would it be possible for you to provide a code snippet for your proposed method?

from react-native-fast-image.

zeekay18 avatar zeekay18 commented on July 18, 2024 1

Is there any plan in adding defaultimage as a placeholder?

from react-native-fast-image.

timothystewart6 avatar timothystewart6 commented on July 18, 2024 1

What's wrong with just adding defaultSource? https://facebook.github.io/react-native/docs/image.html#defaultsource

from react-native-fast-image.

EgidioCaprino avatar EgidioCaprino commented on July 18, 2024 1

@sanealytics what is the first image that you render when !this.state.isImgLoaded? Is that a sort of placeholder? If so, why do have to remove it when the actual image is loaded? If you would not remove it that the placeholder would remain when some error occurs while displaying the actual image.

from react-native-fast-image.

zackhsuan avatar zackhsuan commented on July 18, 2024 1

I feel quite upset that this placeholder feature is still not implemented yet white SDWebImages lib supports it since the very early release. SAD

from react-native-fast-image.

lucasfeijo avatar lucasfeijo commented on July 18, 2024 1

@codingwaysarg I've rewritten your wrapper to suit my needs and I think it's cleaner: https://gist.github.com/lucasfeijo/8dc8cdb52558744103cbc3bfe1c617f8

Throwing it out there for anyone that needs a placeholder with fast-image.

from react-native-fast-image.

qalqi avatar qalqi commented on July 18, 2024 1
import React from 'react'

import FastImage from 'react-native-fast-image'
import PLACE_HOLDER_DEFAULT from '../assets/images/placeholder_default.png';


class SmartImage extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            imageLoading: true
        }
    }

    imageLoadProgress = (e) => {
        this.setState({ imageLoading: false })
        this.props.onProgress && this.props.onProgress(e)
    }
    imageLoadError = () => {
        this.setState({ imageLoading: false })
        this.props.onError && this.props.onError()
    }

    imageLoad = (e) => {
        this.setState({ imageLoading: true })
        this.props.onLoad && this.props.onLoad(e)
    }

    render() {
        let { source } = this.props
        const { style, resizeMode } = this.props
        const { imageLoading } = this.state

        source = imageLoading ? source : (this.props.placeholder || PLACE_HOLDER_DEFAULT);

        return (
            <FastImage
                {...this.props}
                style={style}
                source={source}
                fallback={!this.state.imageLoading}
                onError={this.imageLoadError}
                onProgress={this.imageLoadProgress}
                onLoad={this.imageLoad}
            />
        )
    }
}


export default SmartImage

To use elsewhere

<SmartImage style={styles.somestyle}
                  source={{ uri: item.someuri }}  placeholder={{uri: globalplaceholderURL }}
/>

<SmartImage style={styles.somestyle}
                  source={{ uri: item.someuri }}  placeholder={REQUIRE_PLACEHOLDER}
/>

from react-native-fast-image.

sridix avatar sridix commented on July 18, 2024 1

You can use

Working fine with below code.
<ImageBackground source={require('../assets/images/logo.png')} resizeMode="center">
<FastImage
source={{ uri: item.front_image }}
style={{
width: '100%',
height: undefined,
aspectRatio: .7,
}}
resizeMode={'stretch'}
/>

from react-native-fast-image.

zakster12 avatar zakster12 commented on July 18, 2024

I really think that it would be awesome to have a placeholder.

from react-native-fast-image.

gitlovenotwar avatar gitlovenotwar commented on July 18, 2024

Yes, placeholder is definitely a must! Great plugin, btw.

from react-native-fast-image.

pavanmehta91 avatar pavanmehta91 commented on July 18, 2024

@DylanVann any update on the placeholder? Or any workarounds to have a placeholder(defaultImage) ?

from react-native-fast-image.

wellyshen avatar wellyshen commented on July 18, 2024

Great library and waiting for placeholder feature

from react-native-fast-image.

AlirezaAkbarix avatar AlirezaAkbarix commented on July 18, 2024

@Angelk90! That's not a wise solution! It's better to handle with onLoadEnd method.

from react-native-fast-image.

Angelk90 avatar Angelk90 commented on July 18, 2024

@AlirezaAkbarix : You could be more specific. Can you give an example?

from react-native-fast-image.

craigcoles avatar craigcoles commented on July 18, 2024

@DylanVann Is there any update on this particular feature? I did see that you mentioned in #109 (comment) that it would probably get added. Is it on the roadmap? 🙏

from react-native-fast-image.

EgidioCaprino avatar EgidioCaprino commented on July 18, 2024

What's wrong with just adding defaultSource? https://facebook.github.io/react-native/docs/image.html#defaultsource

@timothystewart6 so is it just a matter of naming? I think a placeholder prop is more suitable for this case.

** Edit **
Now that I've read the request more this won't address the placeholder feature but it is a workaround for a fallback so I'll leave the comment. Hope this helps.

I've managed to do it like this:

      <FastImage          
          source={{
            uri: `https://www.example.com/example.png`,
          }}
          fallback
          defaultSource={require(../images/fallback.png))}
        />

I don't think defaultSource works with <FastImage /> but it does with <Image /> so I set fallback so that on an error, it falls back to <Image /> which supports defaultSource

@timothystewart6 that is great, but covers only the case when an error occurs and you still want to display a default image. It does not cover the case where the actual image is being loaded and you want a placeholder in the meantime.

from react-native-fast-image.

timothystewart6 avatar timothystewart6 commented on July 18, 2024

What's wrong with just adding defaultSource? https://facebook.github.io/react-native/docs/image.html#defaultsource

@timothystewart6 so is it just a matter of naming? I think a placeholder prop is more suitable for this case.

Not just naming, it handles both the default and fallback. Wouldn't just passing prop through to the underlying <Image /> component work? Maybe I am misunderstanding. Sorry!

from react-native-fast-image.

sanealytics avatar sanealytics commented on July 18, 2024

That is a good idea @EgidioCaprino but it doesn't work for me.
I load a super low res default of the first image as photo.uri, and then rotate through the array of high res photo.images.
If I don't hide it on first image load, the default photo will flash before the next one does.

Your use case might differ.

from react-native-fast-image.

deepgosalia1 avatar deepgosalia1 commented on July 18, 2024

Solved in both Android and iOS in #550

@danielgindi I went through your forked repo and the fixing of the placeholder issue. Can you please explain the steps in general to replace @DylanVann's npm module in my project with yours? Do i just copy and replace his files with yours in the nodemodule's folder? or is there any other way?

from react-native-fast-image.

muhammad786 avatar muhammad786 commented on July 18, 2024

Seems still no support for a placeholder, following is my workaround for placeholder

<View> { loading && <MImage containerStyle={imageStyle} uri={placeholderUri} /> } <FastImage style={loading?{width:0, height:0}:imageStyle} source={uri} onLoad={()=>{ setTimeout(()=>{ setLoading(false) },100) }} resizeMode={FastImage.resizeMode.contain} /> </View>

from react-native-fast-image.

danielgindi avatar danielgindi commented on July 18, 2024

@deepgosalia1 The steps are "replace the package's version with the git repo url"

from react-native-fast-image.

thanhtrankma avatar thanhtrankma commented on July 18, 2024

Yeah I was thinking of including that but cut it to get the lib released. I'll take a look at how that could be done now.
Seems still no support for a placeholder.

from react-native-fast-image.

URvesh109 avatar URvesh109 commented on July 18, 2024
    <FastImage style={imageStyle} source={{uri: imgSrc}} />
    <Image style={PLACE_HOLDER_IMAGE_STYLE} source={Images.emptyImg} />

export const PLACE_HOLDER_IMAGE_STYLE: ImageStyle = {
position: 'absolute',
width: '100%',
height: '100%',
zIndex: -100,
borderRadius: 10,
};

Temporary solution

from react-native-fast-image.

fukemy avatar fukemy commented on July 18, 2024

i didnt find any perfect solution in above posts, even Glide and SdWebImage provide placeholder it's self

from react-native-fast-image.

DylanVann avatar DylanVann commented on July 18, 2024

It has been merged. Mostly based on @danielgindi's PR (thank you for that, very thorough). Some of the other solutions in this thread are not bad either, but I wouldn't go that route in this lib, left to userland.

There's certain things that the solution that is merged won't support, e.g. different resizeMode for the placeholder, different tint, priority, low resolution networked image, that could be accomplished by something as simple as rendering one FastImage below another FastImage. I think the merged solution does have the advantage of having superior performance though, and it makes use of what's provided by the native libraries this library depends on.

from react-native-fast-image.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.