gpt4 book ai didi

ios - 在我的相机应用程序 ios (xcode) 上,当我拍照时,我按下一个按钮来查看图像,但是当我按下一个按钮返回相机时,她卡住了

转载 作者:行者123 更新时间:2023-11-29 02:26:28 25 4
gpt4 key购买 nike

这是我的viewcontroller.m

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "ShowPhotoViewController.h"
@interface ViewController ()

@end

@implementation ViewController
AVCaptureSession *session;
AVCaptureStillImageOutput *stillImageOutput;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}

-(void)viewWillAppear:(BOOL)animated{
session = [[AVCaptureSession alloc]init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

if ([session canAddInput:deviceInput]){
[session addInput:deviceInput];

}
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.frameForCapture.frame;

[previewLayer setFrame:frame];

[rootLayer insertSublayer:previewLayer atIndex:0];

stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];

[session addOutput:stillImageOutput];

[session startRunning];
}




- (IBAction)takePhoto:(id)sender {
// ShowPhotoViewController* showPhotoCtrl = segue.destinationViewController;
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageDate = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageDate];
self.imageView.image = image;


}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//openSecondScreen

if([segue.identifier isEqualToString:@"goToShow"]){
ShowPhotoViewController* showPhotoCtrl = segue.destinationViewController;
showPhotoCtrl.image = self.imageView.image;

}
}

-(IBAction)backToFirstScreen:(UIStoryboardSegue*)sender{

}


@end

我的 ShowPhotoViewController.m

#import "ShowPhotoViewController.h"

@interface ShowPhotoViewController ()

@end

@implementation ShowPhotoViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.showPhoto.image = self.image;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end

所以当我拍照并按下显示按钮时,他会带我到 ShowPhotoViewController然后在 ShowPhotoViewController 中我按回他带我到 ViewController 但相机卡住

有人有什么建议吗?

谢谢你!

最佳答案

这可能是由于您从 ShowPhotoViewController 弹出时重新分配和设置,因为初始化设置已写入-(void)viewWillAppear:(BOOL)animated 方法。因为只要 View 出现,就会调用此方法。因此,最好通过使用一些标志(如 isFirstTime)来管理初始化方法只工作一次。

关于ios - 在我的相机应用程序 ios (xcode) 上,当我拍照时,我按下一个按钮来查看图像,但是当我按下一个按钮返回相机时,她卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com