I'm trying to make a simple console program for macOS, which should take one image with built in camera and save it to photo library.
Here is a complete code for that program:
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
volatile int a = 0;
@interface PhotoCaptureProcessor: NSObject <AVCapturePhotoCaptureDelegate>
@end
@implementation PhotoCaptureProcessor
- (void) captureOutput:(AVCapturePhotoOutput *)output
didFinishProcessingPhoto:(AVCapturePhoto *)photo
error:(NSError *)error {
NSLog(@"Called\n");
if(error) {
return;
}
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto
data:[photo fileDataRepresentation]
options:nil];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if(error) {
NSLog(@"Error saving photo\n");
}
a = 1;
}];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *inputVideoDevice = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
[captureSession beginConfiguration];
if(inputVideoDevice) {
if([captureSession canAddInput:inputVideoDevice]) {
[captureSession addInput:inputVideoDevice];
}
}
else {
return 1;
}
AVCapturePhotoOutput *photoOutput = [[AVCapturePhotoOutput alloc] init];
if(photoOutput) {
if([captureSession canAddOutput:photoOutput]) {
[captureSession addOutput:photoOutput];
}
}
captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
[captureSession commitConfiguration];
[captureSession startRunning];
while(![captureSession isRunning]){
NSLog(@"Still not running\n");
}
AVCapturePhotoSettings *photoSettings = [[AVCapturePhotoSettings alloc] init];
PhotoCaptureProcessor *photoProcessor = [[PhotoCaptureProcessor alloc] init];
[photoOutput capturePhotoWithSettings:photoSettings delegate:photoProcessor];
while(!a){
}
}
return 0;
}
After calling method startRunning
little light on my Mac camera turns green (which I believe tells that camera is in use). Unfortunately, method captureOutput
from PhotoCaptureProcessor
, which is my delegate, is never called. If I right click on captureOutput
and then click on Go To Definition it takes me to captureOutput
in AVCapturePhotoCaptureDelegate
(so I believe method signature is fine).
Can anyone tell what am I doing wrong?