File size: 1,066 Bytes
947c08e
 
4af0f6c
 
947c08e
4af0f6c
 
 
 
 
 
 
947c08e
 
4af0f6c
947c08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Image as RNImage } from 'react-native';

export const blobToBase64 = (blob: Blob, mimetype: string = ""): Promise<string> => {
  return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.onloadend = () => {
          const base64String = reader.result as string;
          if (mimetype){
            const base64WithMimeType = `data:${mimetype};base64,${base64String.split(',')[1]}`;
            resolve(base64WithMimeType);
          }else resolve(base64String);
      };
      reader.onerror = reject;
      reader.readAsDataURL(blob);
  });
};

export const base64ToBlob = (base64: string, mimetype: string = 'image/png'): Blob => {
  const byteCharacters = Buffer.from(base64, 'base64');
  return new Blob([byteCharacters], { type: mimetype });
};

export const getImageLayout = (uri: string): Promise<{ width: number; height: number }> => {
  return new Promise((resolve, reject) => {
    RNImage.getSize(
      uri,
      (width, height) => resolve({ width, height }),
      (error) => reject(error)
    );
  });
};