首页 > 解决方案 > 如何在 React Native Expo 上显示字节数组图像?

问题描述

我可以byte-array在 Swagger 上查看图像,但不能在 React Native 上查看。我已经尝试将它转换为base64并且它可以工作,但我想知道如何将它显示在byte-array.

@GetMapping(value = "/{id}", produces = { "image/jpg", "image/jpeg", "image/png" })
public ResponseEntity<Resource> getImage(@PathVariable final UUID id){
    final User user =  userService.findById(id).orElseThrow(() -> new ResourceNotFoundException("User Not Found."));
    Optional<Image> image = imageService.findByUser(user);
    if(image.isPresent()) {
        byte[] bytes = Files.readAllBytes(Paths.get(image.get().getSignDocPath()));
        return ResponseEntity.ok().body(new ByteArrayResource(bytes));
    }
}
<Image
  source={{ uri: imageItem.url }}
  style={styles.imagePreview}
/>

标签: arraysspring-bootreact-nativeexpo

解决方案


你可以使用这个:

const getImageSource = () => {
    return `data:image/jpeg;base64,${source}`
}

<Image style = {styles.image} source = {{uri: getImageSource()}}/>

推荐阅读