Compressing base64 encoded images in React-Native on Android does not recognise 'data' protocol

前端 未结 1 1752
失恋的感觉
失恋的感觉 2021-01-22 01:18

Issue

Within a React-Native (0.43) application we are using a component that uses a SectionList to render photos sorted by day. Each section can contain

相关标签:
1条回答
  • 2021-01-22 01:45

    I managed to resolve the issue with the use of react-native-fetch-blob and react-native-image-resizer for both iOS and Android. Performance is unexpectedly good in comparison to the implementation in the question above. I shared the code below for others to use :)

    export function compressPhoto(photo) {
    return new Promise((resolve, reject) => {
    
        let tempUri = `${cache}/Vire_${photo.data.userPhotoDate}.jpg`
    
        fs.writeFile(tempUri, photo.data.userPhoto, "base64")
            .then(() => {
                ImageResizer.createResizedImage(
                    `file:${tempUri}`, IMAGE_TARGET_SIZE, IMAGE_TARGET_SIZE, "JPEG", 100, 0).then((resizedImageUri) => {
                    fs.readFile(`${resizedImageUri}`, "base64")
                        .then( data => {
                            resolve({...photo, data: { ...photo.data, userPhoto: data }})
                        })
                        .catch(error => reject(`readFile:error: ${error}`))
                },
                (error) => reject(`createResizedImage:error: ${error}`)
                )
            })
            .catch( error => {
                reject(`writeFile:error: ${error}`)
            })
    
         })
    }
    

    The gist is storing base64 encoded picture in the cache-directory and using imageResizer to fetch the image, compress it, and read it again in base64 for use.

    0 讨论(0)
提交回复
热议问题