-
Notifications
You must be signed in to change notification settings - Fork 16
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
Conversation
Sources/Data/PhotoStorage.swift
Outdated
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) |
There was a problem hiding this comment.
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) | ||
} |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
d56578d
to
7e17d3a
Compare
|
||
imageManager.requestImageData(for: asset, options: imageRequestOptions) { (imageData, _, _, _) in | ||
guard let imageData = imageData else { return } | ||
self.createAssetFromImageData(imageData as Data, completion: completion) |
There was a problem hiding this comment.
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?) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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") } |
There was a problem hiding this comment.
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))") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about weak self
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work @robinsalehjan 💪
Why?
captureImage
method in favour of delegationprivate
methods into extensionsfileprivate
modifier in favour ofprivate