Add SDK Dependency
Method A: CocoaPods Integration (Recommended)
Add the Facebetter dependency to your project’s Podfile:
Run the installation command:
Xcode 15+ Compilation Error HandlingIf you are using Xcode 15 or later, you might encounter a Sandbox: rsync.samba deny(1) error during compilation. This is caused by Xcode’s default User Script Sandboxing being enabled.Solution:
- Select your Project in Xcode.
- Navigate to the Build Settings tab.
- Search for
ENABLE_USER_SCRIPT_SANDBOXING.
- Change its value from
Yes to No.
Method B: Manual Framework Integration
Go to the Download page to get the latest SDK, then extract it.
Copy the Facebetter.framework library from the SDK package to your project path.
Open Xcode and refer to this guide to add the Facebetter.framework dynamic library. Make sure the Embed property of the added dynamic library is set to Embed & Sign.
Permission Configuration
Add necessary permissions in Info.plist:
Permission Descriptions:
- Camera Permission: Optional. Only needed when using camera capture for beauty processing in the app. Not required if only processing existing images.
Log Configuration
Logging is disabled by default and can be enabled as needed. Both console logging and file logging switches are supported.
Logging should be enabled before creating the beauty engine, otherwise you may not see initialization logs.
Create Configuration Engine
Follow the instructions on this page to get your appid and appkey.
Verification Priority:
- If
licenseJson is provided, use license data verification (supports online response and offline license)
- Otherwise, use
appId and appKey for automatic online verification
Error Handling
After creating the engine, it’s recommended to check if it was successful:
Adjust Beauty Parameters
All beauty parameters range from [0.0, 1.0]. Set to 0 to disable the effect.
Set Skin Beauty Parameters
Use the setBasicParam interface to set skin beauty parameters. Parameter range [0.0, 1.0].
Supported skin beauty parameters:
Set Skin-Only Beauty
Use the setSkinOnlyBeauty: interface to set whether beauty effects are applied only to skin regions. When enabled, beauty effects (smoothing, whitening, etc.) will only be applied to detected skin areas, leaving non-skin areas unchanged.
After enabling skin-only beauty, even with high beauty parameter values, non-skin areas (such as background, clothing, etc.) will not be affected.
Set Face Reshape Parameters
Use the setReshapeParam interface to set face reshape parameters. Parameter range [0.0, 1.0].
Supported face reshape parameters:
Set Makeup Parameters
Supported makeup parameters:
Set Virtual Background
Enable virtual background through the setVirtualBackground interface:
Using Filters and Stickers
Filter Functionality
Filters are set through the setFilter: interface. Filter resource files (.fbd) must be registered via registerFilter:fbdFilePath: first.
Sticker Functionality
Stickers are set through the setSticker: interface and also need to be registered first.
Set Engine Callbacks
Monitor engine events (license validation and engine initialization status):
Event codes:
FBEngineEventCodeLicenseValidationSuccess (0): License validation succeeded
FBEngineEventCodeLicenseValidationFailed (1): License validation failed
FBEngineEventCodeInitializationComplete (100): Engine initialization completed
FBEngineEventCodeInitializationFailed (101): Engine initialization failed
Process Images
Create Images
Image data is encapsulated through FBImageFrame, supporting formats: YUVI420, NV12, NV21, RGB, RGBA, BGR, BGRA.
Create FBImageFrame with RGBA
Create FBImageFrame with image file
Rotate Images
FBImageFrame has built-in image rotation methods that can be used as needed.
Rotation angles
Process Images
processMode includes Video and Image modes. Video mode is suitable for live streaming and video scenarios with higher efficiency. Image mode is suitable for image processing scenarios.
The engine automatically maintains input/output format consistency. If input is RGBA format, output is RGBA format; if input is I420 format, output is I420 format.
Get Processed Image Data
Get I420 data
FBImageFrame can be converted to various formats through built-in toXXX methods: YUVI420, NV12, NV21, RGB, RGBA, BGR, BGRA. These methods can be used for format conversion.
External Texture Processing
When using external texture processing, you must ensure the OpenGL ES context is on the main thread and pass externalContext = YES during engine initialization.
Use Cases
External texture processing is suitable for the following scenarios:
- OpenGL ES/Metal Rendering Pipeline Integration: When your application already uses OpenGL ES or Metal for rendering, you can directly use textures as input and output, avoiding CPU-GPU data copying
- Real-time Video Processing: Process textures directly in video rendering callbacks to reduce memory copy overhead
- Performance Optimization: Avoid downloading texture data to CPU memory and uploading back to GPU, improving processing efficiency
Configure External Context
When using external texture processing, you need to enable the externalContext option when creating the engine:
Important Notes:
- When
externalContext = YES, the engine will not create its own OpenGL context, but use the current thread’s OpenGL context
- The engine must be created in a valid OpenGL context
- Input and output textures must be in the same OpenGL context
Create Texture Frame
Use the FBImageFrame.createWithTexture: method to create an image frame from an OpenGL texture:
Parameter Description:
textureId: OpenGL texture ID (type GL_TEXTURE_2D)
width: Texture width (pixels)
height: Texture height (pixels)
stride: Row stride (bytes), usually width * 4 (RGBA format)
Get Output Texture
After processing the image, you can get the output texture through the FBImageBuffer.texture property:
Complete Example
Important Notes
1. Context Requirements
- Engine must be created in a valid OpenGL context: When
externalContext = YES, the engine uses the current thread’s OpenGL context, so the engine must be created in the OpenGL rendering thread
- Context consistency: Input texture, engine processing, and output texture must be in the same OpenGL context
- Thread safety: OpenGL operations must be executed in the same thread
2. Texture Format Requirements
- Input texture format: Supports
GL_RGBA format GL_TEXTURE_2D textures
- Texture parameters: It is recommended to set the following texture parameters for best results:
- Lazy initialization: Initialize the engine in the first rendering callback to ensure it is created in the correct OpenGL context
- Reuse FBImageFrame: If possible, reuse
FBImageFrame objects to reduce object creation overhead
- Process mode selection:
FBProcessModeVideo: Suitable for real-time video stream processing, higher performance
FBProcessModeImage: Suitable for single frame image processing, better quality
4. Memory Management
- ARC automatic management: iOS uses ARC for automatic memory management, but still need to pay attention to releasing unused objects in time
- Texture lifecycle: Output textures are managed by the engine and do not need manual deletion, but input textures need to be managed by the caller
- Avoid circular references: When using
self in blocks or callbacks, pay attention to using __weak to avoid circular references
5. Error Handling
- Check return values: All API calls should check return values
- Nil pointer checks: Check if return values of
createWithTexture: and processImage: are nil
- Texture validity: Ensure input texture ID is valid and bound to the current OpenGL context
6. Common Issues
- Engine creation failure: Check if it is created in an OpenGL context and if
externalContext is correctly set
- Texture processing failure: Check if texture format is RGBA and texture parameters are correctly set
- Context loss: If the OpenGL context is destroyed, the engine needs to be recreated
Lifecycle Management
FBBeautyEffectEngine is a singleton and is automatically released when the app ends. Manual management is not required.
Release Resources
When ViewController is destroyed, be sure to release engine resources:
Memory Management
- Release
FBImageFrame and FBImageBuffer objects timely
- Avoid repeatedly creating large numbers of image objects in loops
- Recommend reusing
FBImageFrame objects