diff --git a/README.MD b/README.MD index 875c2d8..b1b6410 100644 --- a/README.MD +++ b/README.MD @@ -23,7 +23,7 @@ iOS 16.0 or later / macOS 12 or later. ### Installing via Swift Package Manager (SPM) -You can easily integrate OpenGraphReader into your project using Swift Package Manager: +You can easily integrate `RemoveBg` into your project using Swift Package Manager: 1. Open your Xcode project. 2. Click on "File" > "Swift Packages" > "Add Package Dependency..." @@ -35,16 +35,80 @@ You can easily integrate OpenGraphReader into your project using Swift Package M Import the package and use it to interact with the remove.bg API. -Example: +### Basic ```swift import RemoveBg -// Configure the API client -let client = RemoveBGClient(apiKey: "your-api-key") -let result = try await client.removeBackground(fromImageData: image.jpegRepresentation(0.8)) -let imageResult = UIImage(data: result.imageData) +let client = RemoveBgClient(apiKey: "my-api-key") +let result = try await client.removeBackground(fromImageData: imageData) +let image = UIImage(data: result.imageData) +``` + +### Configuration + +You can configure all available removal options using `ApiOptions`: + +```swift +let apiOptions = ApiOptions(size: .preview, bgColor: "#F00") +let result = try await client.removeBackground(fromImageData: loaded, options: apiOptions) +``` + +### Diferent image inputs + +You can also use remote URL or base64 string as image input: + +```swift +try await client.removeBackground(fromFileAtUrl: ...) +try await client.removeBackground(fromBase64Image: ...) +``` + +### Reading result meta + +`ApiResult` contains `meta` object containing all information about request made, such as credits charged or foreground position: + +```swift +result.meta?.creditsCharged +``` + +### Full SwiftUI example + +```swift +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") + } + } + } + } +} ``` ## License