imaginary-cloud / CameraManager

Simple Swift class to provide all the configurations you need to create custom camera view in your app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

imageCompletion called twice for capturePictureWithCompletion with .writeFilesToPhoneLibrary = true

franckclement opened this issue · comments

Hey,

It turns out that when you set

cameraManager.writeFilesToPhoneLibrary = true

and then call

cameraManager.capturePictureWithCompletion {}

The imageCompletion closure get called twice.

Here is a workaround t'ill it's fixed

cameraManager.capturePictureWithCompletion { [weak self] result in
   guard let `self` = self else { return }
   switch result {
      case .success(let content):
        switch content {
          case .image(let photo):
            // update your layout ....
          default: break
         }
       case .failure(let error):
          // Handle error
    }
}

switching over content and using .image case let ou get notified once per image captured, but if you need to use the .asset case then you'll get notified twice.

This happens to me too

Right. As a part of 8d3a8a7 the following lines were added:

library?.save(imageAtURL: filePath, albumName: self.imageAlbumName, date: date, location: location) { asset in
if let asset = asset {
imageCompletion(CaptureResult(asset))
} else {
imageCompletion(.failure(CaptureError.assetNotSaved))
}

I guess we shouldn't trigger the imageCompletion callback there, right? My suggestions: We could either spend some time to include a success/failure information about the optional library storage to the primary callback or simply drop this part and replace it by a simple log line?

@Johnson145 Yeah, I guess a simple log line should be OK.

At the end of this function (line 577):

_capturePicture(_ imageData: Data, _ imageCompletion: @escaping (CaptureResult) -> Void)

There is a callback to imageCompletion escaping closure, and the failure when the image is saved to library is caught, so callbacks seems consistent and not called twice with this little update 👍🏻

That's right. However, I wouldn't keep only that outer callback, because it will get triggered before the optional library storing finished (whether successful or not). That's why I kept the inner callbacks in my pull request. Instead I prevent the outer callback with a simple else clause. The latter is actually exactly the same way which is already applied to video recording.

One may still argue whether it's a good idea that a failure during the library storing should imply a total failure or not. For now, I've just added a short TODO comment in the PR.

I am facing the same issue. Even I followed @franckclement work around.

@objc func captureImage(){
        cameraManager.capturePictureWithCompletion({ [weak self] result in
            guard let `self` = self else { return }

            switch result {
            case .failure:
                self.cameraManager.showErrorBlock("Error occurred", "Cannot save picture.")
            case .success(let content):
                switch content {
                case .image(let photo):
                    self.thumbnailImage.image = photo
                default: break
                }
            }
        })
    }

@franckclement @fenixsolorzano @Johnson145 @tapannathvani

just released v5.1.0 where this should be fixed.