react-native-web icon indicating copy to clipboard operation
react-native-web copied to clipboard

Image.resolveAssetSource doesn't work correctly in RNW

Open TQCasey opened this issue 4 years ago • 8 comments

Image.resolveAssetSource doesn't work correctly in RNW

TQCasey avatar Jul 10 '20 08:07 TQCasey

Thank you for reporting an issue. Please note that an issue must include the information that is marked as REQUIRED below, or it may be closed

https://raw.githubusercontent.com/necolas/react-native-web/master/.github/ISSUE_TEMPLATE/bug.md

necolas avatar Jul 10 '20 09:07 necolas

I figured it out .

Any one need Image.resolveAssetSource ,just try this .

if (Platform.OS == "web") {
	Image.resolveAssetSource = (base64) => {

		// strip tag png 
		if (base64.indexOf ('data:image/png;') >= 0) {
			base64 = base64.substring(base64.indexOf(',') + 1)
	
			const header = atob(base64.slice(0, 50)).slice(16, 24)
			const uint8 = Uint8Array.from(header, c => c.charCodeAt(0))
			const dataView = new DataView(uint8.buffer)
		
			return {
				width: dataView.getInt32(0),
				height: dataView.getInt32(4)
			}
		}
	
		// strip tag for jpeg 
		if (base64.indexOf ('data:image/jpeg;') >= 0) {
			base64 = base64.substring(base64.indexOf(',') + 1)
	
			let bindata = atob (base64);
			const data = Uint8Array.from(bindata, c => c.charCodeAt(0))
	
			var off = 0;
			while (off < data.length) {
				
				while (data [off] == 0xff) off++;
				var mrkr = data [off];  off++;
	
				if (mrkr == 0xd8) continue;    // SOI
				if (mrkr == 0xd9) break;       // EOI
				if (0xd0 <= mrkr && mrkr <= 0xd7) continue;
				if (mrkr == 0x01) continue;    // TEM
	
				var len = (data [off] << 8) | data [off + 1];  
				off += 2;  
	
				if(mrkr == 0xc0) { 
					return {
						type        : 'jpeg',
						bpc         : data [off],     // precission (bits per channel)
						width       : (data [off + 1]<<8) | data [off + 2],
						height      : (data [off + 3]<<8) | data [off + 4],
						cps         : data [off + 5]    // number of color components
					}
				}
				off+=len-2;
			}
		}
	}
}

TQCasey avatar Jul 10 '20 12:07 TQCasey

Thanks @TQCasey ! We also ran into this problem. Just wondering, why is this happening to some people?

Our versions:

  • React Native Web 0.13.16
  • React Native 0.63.3
Screenshot 2020-12-17 at 17 00 37

nabilfreeman avatar Dec 17 '20 17:12 nabilfreeman

Oh yeah, because I'm using react-native-fast-image. Will dig more and write up here if it's more of an issue there or here.

nabilfreeman avatar Dec 17 '20 17:12 nabilfreeman

If you need width and height of an image, you should use on RN web Image.getSize instead of Image.resolveAssetSource

require(file/path) will be converted to local file url and it will work fine.

Image.getSize(require('path/to/file'), callback)

ApayRus avatar Apr 23 '21 21:04 ApayRus

Having same issue in this example reported by a user: https://snack.expo.dev/@ageron/tfjs-test-2

Can anyone advise?

jasonmayes avatar Sep 23 '21 21:09 jasonmayes

Hi, I'm currently working on VideoPlayer component that uses 'react-native-video' library which uses 'resolveAssetSource' function.

Can anyone advise how to overcome this issue? Are there any plans to solve this issue?

adids1221 avatar Apr 17 '23 07:04 adids1221

Hi, I'm currently working on VideoPlayer component that uses 'react-native-video' library which uses 'resolveAssetSource' function.

Can anyone advise how to overcome this issue? Are there any plans to solve this issue?

@adids1221 , I resolved your specific issue in your specific https://github.com/necolas/react-native-web/issues/2509#issuecomment-1647535717

flyskywhy avatar Jul 24 '23 09:07 flyskywhy