我一直在寻找解决这个问题的方法,但遗憾的是我做不到:代表永远不会被调用。我尝试了很多可能的方法,但它们都不起作用。
这是使用的代码
#import "headers/FMDManager.h"
@implementation capture
- (AVCaptureDevice *)frontFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices){
if (device.position == AVCaptureDevicePositionFront){
captureDevice = device;
break;
}
}
return captureDevice;
}
- (AVCaptureDevice *)backFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices){
if (device.position == AVCaptureDevicePositionBack){
captureDevice = device;
break;
}
}
return captureDevice;
}
- (void)setupCaptureSession:(BOOL)isfront{
self.session = [[AVCaptureSession alloc] init];
self.session.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureDevice *device = nil;
NSError *error = nil;
if (isfront){
device = [self frontFacingCameraIfAvailable];
} else {
device = [self backFacingCameraIfAvailable];
}
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"FMDLog - ERROR: trying to open camera: %@", error);
exit(0);
}
self.photoOutput = [[AVCapturePhotoOutput alloc] init];
self.photoOutput.highResolutionCaptureEnabled = YES;
self.photoOutput.livePhotoCaptureEnabled = NO;
if ([self.session canAddInput:input] && [self.session canAddOutput:self.photoOutput]) {
[self.session addInput:input];
[self.session addOutput:self.photoOutput];
[self.session startRunning];
NSLog(@"FMDLog - CaptureSession is now running.");
}
}
- (void)takePhoto {
NSLog(@"FMDLog - Session Status: %@", self.session);
NSLog(@"FMDLog - TAKING PICTURE...");
AVCapturePhotoSettings *settings = [[AVCapturePhotoSettings alloc] init];
settings.highResolutionPhotoEnabled = YES;
[self.photoOutput capturePhotoWithSettings:settings delegate:self];
NSLog(@"FMDLog - DONE TAKING PICTURE!");
}
#pragma mark - AVCapturePhotoCaptureDelegate
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {
NSLog(@"FMDLog - captureOutput is now running.");
if (error) {
NSLog(@"error : %@", error.localizedDescription);
}
NSData *imageData = [photo fileDataRepresentation];
if (imageData) {
printf("Saving image to %s...\n", [filepath UTF8String]);
NSLog(@"FMDLog - saving image...");
[imageData writeToFile:filepath atomically:YES];
}
NSLog(@"FMDLog - captureOutput is now ended.");
[self.session stopRunning];
}
- (void)setfilename:(NSString *)filename {
filepath = filename;
}
// NEW
- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishRecordingLivePhotoMovieForEventualFileAtURL:(NSURL *)outputFileURL resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings
{
NSLog(@"FMDLog - NEW METHOD 1");
}
- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingLivePhotoToMovieFileAtURL:(NSURL *)outputFileURL duration:(CMTime)duration photoDisplayTime:(CMTime)photoDisplayTime resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings error:(NSError *)error
{
NSLog(@"FMDLog - NEW METHOD 2");
}
- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishCaptureForResolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings error:(NSError *)error
{
NSLog(@"FMDLog - NEW METHOD 3");
}
-(void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(AVCaptureBracketedStillImageSettings *)bracketSettings error:(NSError *)error {
NSLog(@"FMDLog - NEW METHOD 4");
}
@end
标头
#import <AVFoundation/AVFoundation.h>
// To take Picture
@interface capture : NSObject<AVCapturePhotoCaptureDelegate> {
NSString *filepath;
}
@property (nonatomic,strong) AVCaptureSession *session;
@property (readwrite,retain) AVCapturePhotoOutput *photoOutput;
// @property (nonatomic, copy) void(^returnBlock)(UIImage* image);
- (AVCaptureDevice *)frontFacingCameraIfAvailable;
- (void)setfilename:(NSString *)filename;
- (void)setupCaptureSession:(BOOL)isfront;
- (void)takePhoto;
// - (void)captureWithBlock:(void(^)(UIImage* image))block;
@end