Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
Fix memory leak issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tikoyes94 committed Jul 9, 2017
1 parent eeb8a73 commit d8af584
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
9 changes: 6 additions & 3 deletions CameraEngine/CameraEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ public class CameraEngine: NSObject {
if (!UIDevice.current.isGeneratingDeviceOrientationNotifications) {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
}
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIDeviceOrientationDidChange, object: nil, queue: OperationQueue.main) { (_) -> Void in
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIDeviceOrientationDidChange, object: nil, queue: OperationQueue.main) { [weak self] (_) -> Void in
guard let `self` = self else { return }
self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.orientationFromUIDeviceOrientation(UIDevice.current.orientation)
}
}
Expand Down Expand Up @@ -468,15 +469,17 @@ public extension CameraEngine {

public func startRecordingVideo(_ url: URL, blockCompletion: @escaping blockCompletionCaptureVideo) {
if self.isRecording == false {
self.sessionQueue.async(execute: { () -> Void in
self.sessionQueue.async(execute: {[weak self] () -> Void in
guard let `self` = self else { return }
self.cameraOutput.startRecordVideo(blockCompletion, url: url)
})
}
}

public func stopRecordingVideo() {
if self.isRecording {
self.sessionQueue.async(execute: { () -> Void in
self.sessionQueue.async(execute: {[weak self] () -> Void in
guard let `self` = self else { return }
self.cameraOutput.stopRecordVideo()
})
}
Expand Down
3 changes: 2 additions & 1 deletion CameraEngine/CameraEngineVideoEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ class CameraEngineVideoEncoder {
self.videoInputWriter.markAsFinished()
self.audioInputWriter.markAsFinished()

self.assetWriter.finishWriting { () -> Void in
self.assetWriter.finishWriting {[weak self] () -> Void in
guard let `self` = self else { return }
if let blockCompletion = blockCompletion {
blockCompletion(self.assetWriter.outputURL, nil)
}
Expand Down
9 changes: 6 additions & 3 deletions CameraEngineExample/CameraEngineExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ class ViewController: UIViewController {
@IBAction func takePhoto(_ sender: AnyObject) {
switch self.mode {
case .Photo:
self.cameraEngine.capturePhoto { (image , error) -> (Void) in
self.cameraEngine.capturePhoto {[weak self] (image , error) -> (Void) in
if let image = image {
CameraEngineFileManager.savePhoto(image, blockCompletion: { (success, error) -> (Void) in
guard let `self` = self else { return }
if success {
let alertController = UIAlertController(title: "Success, image saved !", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
self.present(alertController, animated: true, completion: nil)
}
})
}
Expand All @@ -133,9 +134,11 @@ class ViewController: UIViewController {
if !self.cameraEngine.isRecording {
if let url = CameraEngineFileManager.temporaryPath("video.mp4") {
self.buttonTrigger.setTitle("stop recording", for: .normal)
self.cameraEngine.startRecordingVideo(url, blockCompletion: { (url: URL?, error: NSError?) -> (Void) in
self.cameraEngine.startRecordingVideo(url, blockCompletion: {[weak self] (url: URL?, error: NSError?) -> (Void) in
if let url = url {
DispatchQueue.main.async {
guard let `self` = self else { return }

self.buttonTrigger.setTitle("start recording", for: .normal)
CameraEngineFileManager.saveVideo(url, blockCompletion: { (success: Bool, error: Error?) -> (Void) in
if success {
Expand Down

0 comments on commit d8af584

Please sign in to comment.