2021-05-06 14:11:55 +02:00
---
id: frame-processors-plugins-ios
2021-06-28 15:56:49 +02:00
title: Creating Frame Processor Plugins
2021-05-06 14:11:55 +02:00
sidebar_label: Creating Frame Processor Plugins (iOS)
---
2023-09-26 11:39:17 +02:00
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
2021-05-06 14:11:55 +02:00
2021-06-28 15:56:49 +02:00
## Creating a Frame Processor Plugin for iOS
2021-05-06 14:11:55 +02:00
The Frame Processor Plugin API is built to be as extensible as possible, which allows you to create custom Frame Processor Plugins.
2023-07-21 17:52:30 +02:00
In this guide we will create a custom Face Detector Plugin which can be used from JS.
2021-05-06 14:11:55 +02:00
iOS Frame Processor Plugins can be written in either **Objective-C** or **Swift**.
2022-07-07 14:06:31 +02:00
### Automatic setup
2023-02-13 15:22:45 +01:00
Run [Vision Camera Plugin Builder CLI](https://github.com/mateusz1913/vision-camera-plugin-builder),
2022-07-07 14:06:31 +02:00
```sh
2024-01-12 16:11:20 +01:00
npx vision-camera-plugin-builder@latest ios
2022-07-07 14:06:31 +02:00
```
:::info
2023-07-21 17:52:30 +02:00
The CLI will ask you for the path to project's .xcodeproj file, name of the plugin (e.g. `FaceDetectorFrameProcessorPlugin`), name of the exposed method (e.g. `detectFaces`) and language you want to use for plugin development (Objective-C, Objective-C++ or Swift).
2022-07-07 14:06:31 +02:00
For reference see the [CLI's docs](https://github.com/mateusz1913/vision-camera-plugin-builder#%EF%B8%8F-options).
:::
### Manual setup
2021-05-06 14:11:55 +02:00
<Tabs
defaultValue="objc"
values={[
{label: 'Objective-C', value: 'objc'},
{label: 'Swift', value: 'swift'}
]}>
<TabItem value="objc">
1. Open your Project in Xcode
2023-07-21 17:52:30 +02:00
2. Create an Objective-C source file, for the Face Detector Plugin this will be called `FaceDetectorFrameProcessorPlugin.m`.
2021-05-06 14:11:55 +02:00
3. Add the following code:
2023-02-27 11:18:03 +01:00
```objc
2021-05-06 14:11:55 +02:00
#import <VisionCamera/FrameProcessorPlugin.h>
2023-07-21 17:52:30 +02:00
#import <VisionCamera/FrameProcessorPluginRegistry.h>
2021-06-09 10:57:05 +02:00
#import <VisionCamera/Frame.h>
2021-05-06 14:11:55 +02:00
2023-07-21 17:52:30 +02:00
@interface FaceDetectorFrameProcessorPlugin : FrameProcessorPlugin
2021-05-06 14:11:55 +02:00
@end
2023-07-21 17:52:30 +02:00
@implementation FaceDetectorFrameProcessorPlugin
2021-05-06 14:11:55 +02:00
2024-01-12 16:00:36 +01:00
- (instancetype) initWithProxy:(VisionCameraProxyHolder*)proxy
withOptions:(NSDictionary* _Nullable)options {
self = [super initWithProxy:proxy options:options];
2023-07-21 17:52:30 +02:00
return self;
2023-02-27 11:18:03 +01:00
}
2023-07-21 17:52:30 +02:00
- (id)callback:(Frame*)frame withArguments:(NSDictionary*)arguments {
2021-06-09 10:57:05 +02:00
CMSampleBufferRef buffer = frame.buffer;
UIImageOrientation orientation = frame.orientation;
2021-05-06 14:11:55 +02:00
// code goes here
2023-10-03 11:36:55 +02:00
return nil;
2021-05-06 14:11:55 +02:00
}
2023-10-19 10:35:14 +02:00
// highlight-start
VISION_EXPORT_FRAME_PROCESSOR(FaceDetectorFrameProcessorPlugin, detectFaces)
// highlight-end
2021-05-06 14:11:55 +02:00
@end
```
:::note
2023-10-19 11:19:47 +02:00
The Frame Processor Plugin will be exposed to JS through the `VisionCameraProxy` object. In this case, it would be `VisionCameraProxy.initFrameProcessorPlugin("detectFaces")`.
2021-05-06 14:11:55 +02:00
:::
2023-10-19 11:19:47 +02:00
4. **Implement your Frame Processing.** See the [Example Plugin (Objective-C)](https://github.com/mrousavy/react-native-vision-camera/blob/main/package/example/ios/Frame%20Processor%20Plugins/Example%20Plugin/ExampleFrameProcessorPlugin.m) for reference.
2021-06-28 15:56:49 +02:00
2021-05-06 14:11:55 +02:00
</TabItem>
<TabItem value="swift">
2021-06-27 12:37:54 +02:00
1. Open your Project in Xcode
2023-07-21 17:52:30 +02:00
2. Create a Swift file, for the Face Detector Plugin this will be `FaceDetectorFrameProcessorPlugin.swift`. If Xcode asks you to create a Bridging Header, press **create**.
2021-05-06 14:11:55 +02:00

2023-10-03 11:36:55 +02:00
3. In the Swift file, add the following code:
2021-05-06 14:11:55 +02:00
2023-02-27 11:18:03 +01:00
```swift
2023-10-03 11:36:55 +02:00
import VisionCamera
2023-07-21 17:52:30 +02:00
@objc(FaceDetectorFrameProcessorPlugin)
public class FaceDetectorFrameProcessorPlugin: FrameProcessorPlugin {
2024-01-12 16:11:43 +01:00
public override init(proxy: VisionCameraProxyHolder, options: [AnyHashable : Any]! = [:]) {
super.init(proxy: proxy, options: options)
2023-10-19 10:35:14 +02:00
}
2023-10-03 11:36:55 +02:00
public override func callback(_ frame: Frame, withArguments arguments: [AnyHashable : Any]?) -> Any {
2021-06-09 10:57:05 +02:00
let buffer = frame.buffer
let orientation = frame.orientation
2021-05-06 14:11:55 +02:00
// code goes here
2023-10-03 11:36:55 +02:00
return nil
2021-05-06 14:11:55 +02:00
}
}
```
2023-10-03 11:36:55 +02:00
4. Create an Objective-C source file that will be used to automatically register your plugin
2023-02-27 11:18:03 +01:00
```objc
#import <VisionCamera/FrameProcessorPlugin.h>
2023-07-21 17:52:30 +02:00
#import <VisionCamera/FrameProcessorPluginRegistry.h>
2023-02-27 11:18:03 +01:00
2023-10-03 11:36:55 +02:00
#import "YOUR_XCODE_PROJECT_NAME-Swift.h" // <--- replace "YOUR_XCODE_PROJECT_NAME" with the actual value of your xcode project name
2023-02-27 11:18:03 +01:00
2023-10-19 10:35:14 +02:00
// highlight-start
VISION_EXPORT_SWIFT_FRAME_PROCESSOR(FaceDetectorFrameProcessorPlugin, detectFaces)
// highlight-end
2023-02-27 11:18:03 +01:00
```
2023-10-19 11:19:47 +02:00
5. **Implement your frame processing.** See [Example Plugin (Swift)](https://github.com/mrousavy/react-native-vision-camera/blob/main/package/example/ios/Frame%20Processor%20Plugins/Example%20Swift%20Plugin/ExampleSwiftFrameProcessor.swift) for reference.
2021-05-06 14:11:55 +02:00
</TabItem>
</Tabs>
<br />
#### 🚀 Next section: [Finish creating your Frame Processor Plugin](frame-processors-plugins-final) (or [add Android support to your Frame Processor Plugin](frame-processors-plugins-android))