This Swift package is an API wrapper for the remove.bg API. It simplifies the integration of remove.bg's functionality into Swift-based projects, providing an easy and efficient way to access and utilize the API's capabilities.
- Covers all available configuration options.
async
/await
API.- iOS and MacOS support.
- No external dependencies.
iOS 16.0 or later / macOS 12 or later.
You can easily integrate RemoveBg
into your project using Swift Package Manager:
- Open your Xcode project.
- Click on "File" > "Swift Packages" > "Add Package Dependency..."
- Enter the URL of this repository:
https://github.com/pzmudzinski/remove-bg.git
- Choose "Up To Next Major Version": 1.0.0
- Click "Add Package"
Import the package and use it to interact with the remove.bg API.
import RemoveBg
let client = RemoveBgClient(apiKey: "my-api-key")
let result = try await client.removeBackground(fromImageData: imageData)
let image = UIImage(data: result.imageData)
You can configure all available removal options using ApiOptions
:
let apiOptions = ApiOptions(size: .preview, bgColor: "#F00")
let result = try await client.removeBackground(fromImageData: loaded, options: apiOptions)
You can also use remote URL or base64 string as image input:
try await client.removeBackground(fromFileAtUrl: ...)
try await client.removeBackground(fromBase64Image: ...)
ApiResult
contains meta
object containing all information about request made, such as credits charged or foreground position:
result.meta?.creditsCharged
struct ContentView: View {
@State private var avatarItem: PhotosPickerItem?
@State private var image: UIImage?
var body: some View {
VStack {
PhotosPicker("Select image", selection: $avatarItem, matching: .images)
if let image {
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(width: 300, height: 300)
}
}
.onChange(of: avatarItem) {
Task {
if let loaded = try? await avatarItem?.loadTransferable(type: Data.self) {
do {
let client = RemoveBgClient(apiKey: "my-api-key")
let result = try await client.removeBackground(fromImageData: loaded)
self.image = UIImage(data: result.imageData)
} catch {
print(error)
}
} else {
print("Failed")
}
}
}
}
}
This project is licensed under the MIT License - see the LICENSE file for details.