Skip to content

Commit 9e152fe

Browse files
authored
feat: getImage iOS (#253)
* feat: getImage iOS * Support more image types including GIF * use itemProviders * use identifier that comforms to UTTypeImage
1 parent 239948a commit 9e152fe

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ _setContent() {
215215
| ------- | -------- | -------- | ----------------------------------------- |
216216
| content | string[] | Yes | The content to be stored in the clipboard |
217217

218-
219218
#### `hasString()`
220219

221220
```jsx
@@ -255,8 +254,6 @@ static hasWebURL()
255254
Returns whether the clipboard has a WebURL(UIPasteboardDetectionPatternProbableWebURL) content.
256255
Can check if there is a WebURL content in clipboard without triggering PasteBoard notification for iOS 14+
257256

258-
259-
260257
### useClipboard
261258

262259
`useClipboard` is a utility hooks for the `Clipboard` module. `data` contains the content stored in the clipboard.
@@ -283,7 +280,7 @@ export const HooksSample = () => {
283280
static getImage()
284281
```
285282

286-
Get content of image in base64 string type, this method returns a `Promise`, so you can use following code to get clipboard content (ANDROID only)
283+
Get content of image in base64 string type, this method returns a `Promise`, so you can use following code to get clipboard content (iOS and Android Only)
287284

288285
```jsx
289286
async _getContent() {

ios/RNCClipboard.mm

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import "RNCClipboard.h"
22

3-
3+
#import <MobileCoreServices/MobileCoreServices.h>
4+
#import <MobileCoreServices/UTType.h>
45
#import <UIKit/UIKit.h>
56
#import <React/RCTBridge.h>
67
#import <React/RCTEventDispatcher.h>
@@ -215,7 +216,22 @@ - (void) listener:(NSNotification *) notification
215216

216217
RCT_EXPORT_METHOD(getImage:(RCTPromiseResolveBlock)resolve
217218
reject:(__unused RCTPromiseRejectBlock)reject){
218-
reject(@"Clipboard:getImage", @"getImage is not supported on iOS", nil);
219+
UIPasteboard *clipboard = [UIPasteboard generalPasteboard];
220+
NSString *withPrefix;
221+
for (NSItemProvider *itemProvider in clipboard.itemProviders) {
222+
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
223+
for (NSString *identifier in itemProvider.registeredTypeIdentifiers) {
224+
if (UTTypeConformsTo((__bridge CFStringRef)identifier, kUTTypeImage)) {
225+
NSString *MIMEType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)identifier, kUTTagClassMIMEType);
226+
NSString *imageDataBase64 = [[clipboard dataForPasteboardType:identifier] base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
227+
withPrefix = [NSString stringWithFormat:@"data:%@;base64,%@", MIMEType, imageDataBase64];
228+
break;
229+
}
230+
}
231+
break;
232+
}
233+
}
234+
resolve((withPrefix ? : NULL));
219235
}
220236

221237
RCT_EXPORT_METHOD(addListener : (NSString *)eventName) {

src/Clipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const Clipboard = {
7171
NativeClipboard.setImage(content);
7272
},
7373
/**
74-
* (Android Only)
74+
* (iOS and Android Only)
7575
* Get clipboard image in base64, this method returns a `Promise`, so you can use following code to get clipboard content
7676
* ```javascript
7777
* async _getContent() {

src/NativeClipboardModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface Spec extends TurboModule {
5959
*/
6060
setImage(content: string): Promise<void>;
6161
/**
62-
* (Android Only)
62+
* (iOS and Android Only)
6363
* Get clipboard image in base64, this method returns a `Promise`, so you can use following code to get clipboard content
6464
* ```javascript
6565
* async _getContent() {

0 commit comments

Comments
 (0)