Skip to content

Commit

Permalink
Merge pull request #7 from Athlee/develop-v0.2.8
Browse files Browse the repository at this point in the history
Develop v0.2.8
  • Loading branch information
mozharovsky authored Feb 1, 2017
2 parents 009544c + f467a45 commit 1c453c5
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions Source/PhotoCapturable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,44 @@
//

import UIKit
import Photos
import AVFoundation

///
/// Provides still image capturing features.
///
public protocol PhotoCapturable: Capturable {

/// Captures a still image from the current input.
///
/// Captures a still image from the current input.
///
/// - parameter handler: A handler that is called when the image is taken. Default value is `nil`.
/// - Parameters:
/// - saving: Indicates if taken image should be saved to albums.
/// - handler: A handler that is called when the image is taken. Default value is `nil`.
func captureStillImage(saving: Bool, handler: ((UIImage) -> Void)?)

/// This function is optional. It is called when
/// captured still image could not
/// be saved to albums.
///
func captureStillImage(_ handler: ((UIImage) -> Void)?)
/// - Parameter error: Photo Library change error.
func captureStillImageFailed(with error: Error?)
}

// MARK: - Default implementations

public extension PhotoCapturable {

///
/// Captures a still image from the current input.
///
/// - parameter handler: A handler that is called when the image is taken. Default value is `nil`.
///
func captureStillImage(_ handler: ((UIImage) -> Void)? = nil) {
/// - Parameters:
/// - saving: Indicates if taken image should be saved to albums.
/// - handler: A handler that is called when the image is taken. Default value is `nil`.
func captureStillImage(saving: Bool = true, handler: ((UIImage) -> Void)?) {
guard let imageOutput = imageOutput else {
return
}


// TODO: Refactor this code

DispatchQueue.global(qos: DispatchQoS.userInitiated.qosClass).async(execute: {
let videoConnection = imageOutput.connection(withMediaType: AVMediaTypeVideo)
Expand All @@ -61,9 +70,19 @@ public extension PhotoCapturable {
)

DispatchQueue.main.async {
let resizedImage = UIImage(cgImage: imageRef!, scale: sw/iw, orientation: image.imageOrientation)
let resizedImage = UIImage(cgImage: imageRef!, scale: sw / iw, orientation: image.imageOrientation)
handler?(resizedImage)

if saving {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: resizedImage)
}, completionHandler: { [weak self] (success, error) in
if !success {
self?.captureStillImageFailed(with: error)
}
})
}

self.session = nil
self.device = nil
self.imageOutput = nil
Expand All @@ -75,4 +94,10 @@ public extension PhotoCapturable {
})
}

/// This function is optional. It is called when
/// captured still image could not
/// be saved to albums.
///
/// - Parameter error: Photo Library change error.
func captureStillImageFailed(with error: Error?) { }
}

0 comments on commit 1c453c5

Please sign in to comment.