Skip to content

Commit

Permalink
release '0.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckLetellier committed May 5, 2021
2 parents 81af4fc + 45c5bdf commit 9a41a1b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class ViewController: UIViewController, UINavigationControllerDelegate {
}

func removeBackground(_ originalImage: UIImage) {
let resultViewController = EditingViewController(image: originalImage,
apiKey: K.photoRoomAPIKey,
completionHandler: onImageEdited)
let resultViewController = PhotoRoomViewController(image: originalImage,
apiKey: K.photoRoomAPIKey) { [weak self] image in
self?.onImageEdited(image)

}
present(resultViewController, animated: true)
}

Expand Down
4 changes: 2 additions & 2 deletions Example/PhotoRoomKitDemo/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- PhotoRoomKit (0.0.1)
- PhotoRoomKit (0.0.2)

DEPENDENCIES:
- PhotoRoomKit (from `../../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../../"

SPEC CHECKSUMS:
PhotoRoomKit: fe0eb2b1760e36a57685d8fda81456edb7d83b63
PhotoRoomKit: 92fce543303acefcdc6ec194108de2f1c9402cc1

PODFILE CHECKSUM: 75a88f0e658c982c9ca2e49331e2562d62c6ab27

Expand Down
2 changes: 1 addition & 1 deletion PhotoRoomKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "PhotoRoomKit"
s.summary = "Fast background removal API."
s.version = "0.0.2"
s.version = "0.0.3"
s.homepage = "https://github.com/PhotoRoom/PhotoRoomKit-Swift"
s.license = { :type => 'MIT' }
s.author = { "PhotoRoom" => "[email protected]" }
Expand Down
8 changes: 4 additions & 4 deletions PhotoRoomKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

/* Begin PBXBuildFile section */
95097DE52641494A00854E2B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 95097DE42641494A00854E2B /* Assets.xcassets */; };
95097E2A2642C83900854E2B /* PhotoRoomViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95097E292642C83900854E2B /* PhotoRoomViewController.swift */; };
9571030B26412ACF00319C32 /* SegmentationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9571030826412ACF00319C32 /* SegmentationService.swift */; };
9571030C26412ACF00319C32 /* ImageHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9571030926412ACF00319C32 /* ImageHelpers.swift */; };
9571030D26412ACF00319C32 /* EditingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9571030A26412ACF00319C32 /* EditingViewController.swift */; };
D284B1461F7908B300D94AF3 /* SharedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D284B1321F7906DA00D94AF3 /* SharedTests.swift */; };
D284B1471F7908B800D94AF3 /* iOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D284B1361F7906DA00D94AF3 /* iOSTests.swift */; };
D5B2E8AA1C3A780C00C0327D /* PhotoRoomKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5B2E89F1C3A780C00C0327D /* PhotoRoomKit.framework */; };
Expand All @@ -28,9 +28,9 @@

/* Begin PBXFileReference section */
95097DE42641494A00854E2B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
95097E292642C83900854E2B /* PhotoRoomViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhotoRoomViewController.swift; sourceTree = "<group>"; };
9571030826412ACF00319C32 /* SegmentationService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SegmentationService.swift; sourceTree = "<group>"; };
9571030926412ACF00319C32 /* ImageHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageHelpers.swift; sourceTree = "<group>"; };
9571030A26412ACF00319C32 /* EditingViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditingViewController.swift; sourceTree = "<group>"; };
D284B12F1F7906DA00D94AF3 /* Info-iOS-Tests.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-iOS-Tests.plist"; sourceTree = "<group>"; };
D284B1321F7906DA00D94AF3 /* SharedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedTests.swift; sourceTree = "<group>"; };
D284B1361F7906DA00D94AF3 /* iOSTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = iOSTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -113,7 +113,7 @@
D5C6296A1C3A809D007F7B7C /* iOS */ = {
isa = PBXGroup;
children = (
9571030A26412ACF00319C32 /* EditingViewController.swift */,
95097E292642C83900854E2B /* PhotoRoomViewController.swift */,
9571030926412ACF00319C32 /* ImageHelpers.swift */,
9571030826412ACF00319C32 /* SegmentationService.swift */,
);
Expand Down Expand Up @@ -249,8 +249,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
95097E2A2642C83900854E2B /* PhotoRoomViewController.swift in Sources */,
9571030B26412ACF00319C32 /* SegmentationService.swift in Sources */,
9571030D26412ACF00319C32 /* EditingViewController.swift in Sources */,
9571030C26412ACF00319C32 /* ImageHelpers.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,52 @@ github "PhotoRoom/PhotoRoomKit"

**PhotoRoomKit** can also be installed manually. Just download and drop `Sources` folders in your project.

## Usage

First you will need an API Key, go to [our API website](https://photoroom.com/api) to get your own PhotoRoom key.

Then, you can either use the provided `PhotoRoomViewController`, or use the API wrapper

#### PhotoRoomViewController

Just present `PhotoRoomViewController` and handle the callback
```swift
func removeBackground(_ originalImage: UIImage) {
let controller = PhotoRoomViewController(image: originalImage,
apiKey: 'YOUR_API_KEY') { [weak self] image in
self?.onImageEdited(image)

}
present(controller, animated: true)
}

func onImageEdited(_ editedImage: UIImage) {
// Handle your segmented image
}
```
When using the built-in view controller, Photoroom attribution is done for you, no need for extra work.

#### API wrapper

You can also use the API wrapper directly.
```swift
let segmentationService = SegmentationService(apiKey: apiKey)
segmentationService.segment(image: originalImage) { (image, error) in
DispatchQueue.main.async {
if let error = error {
// An error occured
}
guard let image = image else {
// No image returned
return
}
// All good
}
}
```

⚠️ If you use the API wrapper, you'll need to provide correct attribution according to [our API guideline](https://www.notion.so/photoroom/API-Documentation-public-4eb3e45d9c814f92b6392b7fd0f1d51f#7ac1c3bd30fd426ea092e126f4b59c77).

## Author

PhotoRoom, [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//

import UIKit

final public class EditingViewController: UIViewController {
/// Ready to use ViewController to remove the background
final public class PhotoRoomViewController: UIViewController {
enum ViewState {
case success
case loading
Expand Down Expand Up @@ -113,7 +113,10 @@ final public class EditingViewController: UIViewController {
}()

// MARK: - Life Cycle

/// - Parameters:
/// - image: The image you want to remove background
/// - apiKey: PhotoRoom API key
/// - completionHandler: Called once the background removal has been completed. Will not return if errored
public init(image: UIImage, apiKey: String, completionHandler: ((UIImage) -> Void)? = nil) {
self.originalImage = image
self.apiKey = apiKey
Expand All @@ -140,7 +143,8 @@ final public class EditingViewController: UIViewController {
viewState = .loading

//remove background
SegmentationService.segment(image: originalImage, apiKey: apiKey, onCompletion: { (image, error) in
let segmentationService = SegmentationService(apiKey: apiKey)
segmentationService.segment(image: originalImage) { (image, error) in
DispatchQueue.main.async {
if let error = error {
self.viewState = .error(error: error)
Expand All @@ -154,9 +158,7 @@ final public class EditingViewController: UIViewController {
self.viewState = .success
})
}
})

// Do any additional setup after loading the view.
}
}

// MARK: - Constraints
Expand Down
44 changes: 29 additions & 15 deletions Sources/iOS/SegmentationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,36 @@
import Foundation
import UIKit

struct SegmentationService {
typealias SegmentationCallback = (UIImage?, Error?) -> Void
/// Service to remove background from any image
public final class SegmentationService {

static func segment(image: UIImage,
apiKey: String,
/// Handler containing the possible segmented image
public typealias SegmentationCallback = (UIImage?, Error?) -> Void

private let apiKey: String
private enum K {
static let hostURL = URL(string: "https://sdk.photoroom.com/v1/segment")!
}

/// - Parameters:
/// - apiKey: PhotoRoom API key
public init(apiKey: String) {
self.apiKey = apiKey
}

/// Segment the image to a white background
/// - Parameters:
/// - image: The image you want to remove background
/// - onCompletion: Called once the segmentation is over. See `SegmentationCallback` for more detail
public func segment(image: UIImage,
onCompletion: @escaping SegmentationCallback) {
let host = "https://sdk.photoroom.com/v1/segment"
if apiKey == "" {

guard apiKey.isEmpty == false else {
onCompletion(nil, SegmentationError.noAPIKey)
return
}
guard let url = URL(string: host) else {
onCompletion(nil, SegmentationError.invalidData)
return
}
var request = URLRequest(url: url)

var request = URLRequest(url: K.hostURL)
request.httpMethod = "POST"
request.timeoutInterval = 30.0

Expand All @@ -36,8 +50,8 @@ struct SegmentationService {
return
}

let boundary = generateBoundary()
let body = createDataBody(with: media, boundary: boundary)
let boundary = Self.generateBoundary()
let body = Self.createDataBody(with: media, boundary: boundary)

request.httpBody = body

Expand Down Expand Up @@ -71,11 +85,11 @@ struct SegmentationService {

}

static func generateBoundary() -> String {
private static func generateBoundary() -> String {
return "Boundary-\(NSUUID().uuidString)"
}

static func createDataBody(with media: Media, boundary: String) -> Data {
private static func createDataBody(with media: Media, boundary: String) -> Data {

let lineBreak = "\r\n"
var body = Data()
Expand Down

0 comments on commit 9a41a1b

Please sign in to comment.