我正在尝试在我的ViewController中对齐UIButton。我无法说出我的UIButton的具体名称是什么,所以我认为这就是为什么我很难将其作为子视图移动的原因。这是我的代码:
-(void)viewDidAppear:(BOOL)animated
{
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
CALayer *viewLayer = self.vImagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
//Modify to fit screen
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 0.0);
CGAffineTransform scale = CGAffineTransformMakeScale(1.333333, 1.333333);
CGAffineTransform rotate = CGAffineTransformMakeRotation(DEGREES_RADIANS(180));
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
self.vImagePreview.transform = transform;
[self.vImagePreview sendSubviewToFront:UIButton];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];
_stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[_stillImageOutput setOutputSettings:outputSettings];
[session addOutput:_stillImageOutput];
[session startRunning];
}
-(IBAction)captureNow {
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;
}
}
NSLog(@"about to request a capture from: %@", _stillImageOutput);
[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
{
// Do something with the attachments.
NSLog(@"attachements: %@", exifAttachments);
} else {
NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
self.vImage.image = image;
//Saved Photo modifications go here
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}];
}
@end
和我的标题,以防万一:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <ImageIO/CGImageProperties.h>
#define DEGREES_RADIANS(angle) ((angle) / 180.0 * M_PI)
@interface ViewController : UIViewController
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
@property(nonatomic, retain) IBOutlet UIImageView *vImage;
@end
一些澄清:
我的按钮在我的故事板上。该按钮应触发CaptureNow方法,拍照。我无法弄清楚如何让它出现在预览图层上。
答案 0 :(得分:0)
由于您的UI按钮位于故事板上,因此不使用
[self.vImagePreview sendSubviewToFront:UIButton];
使用此行在顶部显示UIBUtton 。
[self.view bringSubviewToFront:UIButton];
就在emethod结束时启动会话之前。