Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove warnings and deprecate captureImage method #40

Merged
merged 8 commits into from
Jun 12, 2019

Conversation

robinsalehjan
Copy link
Contributor

@robinsalehjan robinsalehjan commented Jun 7, 2019

Why?

  • We had 20 or so warnings related to using old APIs from iOS 9
  • Deprecate the captureImage method in favour of delegation
  • Move private methods into extensions
  • Remove fileprivate modifier in favour of private
  • Update Xcode with recommended settings for localisation
  • Ran SwiftLint to have consistency across the project

@robinsalehjan robinsalehjan requested a review from a team June 7, 2019 10:06
fileprivate let assetLibrary = ALAssetsLibrary()
private let baseURL: URL
private let queue = DispatchQueue(label: "no.finn.finjonon.disk-cache-writes", attributes: [])
private let resizeQueue = DispatchQueue(label: "no.finn.finjonon.disk-cache-resizes", attributes: DispatchQueue.Attributes.concurrent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be refactored to DispatchQueue(label: "no.finn.finjonon.disk-cache-resizes", attributes: .concurrent) instead?

discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTrueDepthCamera, .builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, position: .unspecified)
} else {
discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .unspecified)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this rather be

let deviceTypes: [AVCaptureDevice.DeviceType]
if #available(iOS 11.2, *) {
    deviceTypes = [.builtInTrueDepthCamera, .builtInDualCamera, .builtInWideAngleCamera]
} else {
    deviceTypes = [.builtInWideAngleCamera]
}

let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: deviceTypes, mediaType: .video, position: .unspecified)

Copy link
Member

@bstien bstien Jun 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking, but I think it makes it a bit easier to read since the only difference is the list of deviceTypes :)

didCaptureImageCompletion = nil
}

if error == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saves you an indentation😄

guard error == nil else {
    NSLog("Failed capturing still images: \(String(describing: error))")
    return
}

if error == nil {
if let sampleBuffer = photoSampleBuffer, let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: nil) {
if let metadata = CMCopyDictionaryOfAttachments(allocator: nil, target: sampleBuffer, attachmentMode: CMAttachmentMode(kCMAttachmentMode_ShouldPropagate)) as NSDictionary? {
DispatchQueue.main.async {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably use [weak self] here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍

if let sampleBuffer = photoSampleBuffer, let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: nil) {
if let metadata = CMCopyDictionaryOfAttachments(allocator: nil, target: sampleBuffer, attachmentMode: CMAttachmentMode(kCMAttachmentMode_ShouldPropagate)) as NSDictionary? {
DispatchQueue.main.async {
if self.didCaptureImageCompletion == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And an if let here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we avoid the ❗️unwrap a couple lines down

@robinsalehjan robinsalehjan force-pushed the fix/remove-deprecation-warnings branch from d56578d to 7e17d3a Compare June 11, 2019 07:06

imageManager.requestImageData(for: asset, options: imageRequestOptions) { (imageData, _, _, _) in
guard let imageData = imageData else { return }
self.createAssetFromImageData(imageData as Data, completion: completion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use weak self here?

@@ -11,17 +11,17 @@ enum CaptureManagerViewfinderMode {
}

protocol CaptureManagerDelegate: AnyObject {
func captureManager(_ manager: CaptureManager, didCaptureImage data: Data?, withMetadata metadata: NSDictionary?)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps didCaptureImageData is more descriptive since it's an instance of Data?

NSLog("Failed capturing still images: \(String(describing: error))")
}
})
self.cameraSettings = self.createCapturePhotoSettingsObject()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use weak self here?

}
})
self.cameraSettings = self.createCapturePhotoSettingsObject()
self.cameraOutput.capturePhoto(with: self.cameraSettings!, delegate: self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we guard cameraSettings instead of force unwrapping?

func createCapturePhotoSettingsObject() -> AVCapturePhotoSettings {
var newCameraSettings: AVCapturePhotoSettings

if let currentCameraSettings = self.cameraSettings {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for self here.


if self.session.canAddOutput(self.stillImageOutput!) {
self.session.addOutput(self.stillImageOutput!)
if self.session.canAddOutput(self.cameraOutput) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use weak self in this closure?

let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: deviceTypes, mediaType: .video, position: .unspecified)
let availableCameraDevices = discoverySession.devices

guard availableCameraDevices.isEmpty == false else { fatalError("No camera devices found") }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather return instead of calling fatalError here, mainly because it's annoying when simulator crashes while opening this view in the app.

}

guard error == nil else {
NSLog("Failed capturing still images: \(String(describing: error))")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how useful it is, but we could also add a delegate method to report back about errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree 👍

createAssetFromImageData(data as Data, completion: { (asset: Asset) in
var mutableAsset = asset
mutableAsset.imageDataSourceType = .camera
self.didAddAsset(mutableAsset)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about weak self?

Copy link
Contributor

@vadymmarkov vadymmarkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @robinsalehjan 💪

@robinsalehjan robinsalehjan merged commit 399ab1d into master Jun 12, 2019
@robinsalehjan robinsalehjan deleted the fix/remove-deprecation-warnings branch June 12, 2019 06:06
This was referenced Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants