사진을 찍고 사진을 앨범에 저장하는 데 AVCaptureSession을 사용합니다. 버튼을 클릭하면 스냅 샷이 생성되어 앨범에 저장됩니다. 그러나 가로 모드를 사용할 때 풍경 모드를 저장하는 버튼을 클릭하면 정지 이미지가 거꾸로 표시됩니다.AVCaptureVideoOrientation 가로 모드로 인해 정지 이미지가 뒤집어짐
코드 : 사진을 촬영하기위한
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setCaptureSession:[[AVCaptureSession alloc] init]];
[self addVideoInputFrontCamera:NO]; // set to YES for Front Camera, No for Back camera
[self addStillImageOutput];
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] ];
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CGRect layerRect = [[[self view] layer] bounds];
[[self previewLayer]setBounds:layerRect];
[[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[self previewLayer]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveImageToPhotoAlbum) name:kImageCapturedSuccessfully object:nil];
[[self captureSession] startRunning];
camera=[UIButton buttonWithType:UIButtonTypeCustom];
[camera setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
[camera setFrame:CGRectMake(150, 10, 40, 30)];
[camera addTarget:self action:@selector(takephoto:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:camera];
}
버튼 :
-(void)takephoto:(id)sender{
[self captureStillImage];
}
- (void)captureStillImage
{
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
NSLog(@"about to request a capture from: %@", [self stillImageOutput]);
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments) {
NSLog(@"attachements: %@", exifAttachments);
} else {
NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self setStillImage:image];
// [image release];
[[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
}];
}
내가 읽고 제안 : [AVCaptureVideoOrientation 풍경 모드가 거꾸로 정지 영상을 초래할 않는 이유는 무엇입니까? (http://stackoverflow.com/questions/7845520/why-does-avcapturevideoorientation-landscape-modes-result- in-upside-down-still-i? rq = 1) – foundry
@HeWas : 나는 이미 그것을 보았다. 하지만 어떻게 할 생각이 들지 않았어? – user2474320
@ user2474320 ... 하루 종일 오프라인 상태가되지만 오늘 밤 유용한 대답이 없으면 더 자세한 내용을 알려 드리겠습니다. – foundry