#import <AVFoundation/AVFoundation.h>
@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIButton *cameraButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:self.imageView];
self.cameraButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.cameraButton setTitle:@"拍照" forState:UIControlStateNormal];
[self.cameraButton setFrame:CGRectMake(100, 100, 100, 40)];
[self.view addSubview:self.cameraButton];
[self.cameraButton addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)takePhoto:(id)sender {
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
if ([device canCaptureVideo]) {
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
NSLog(@"%@", error);
return;
}
if ([captureSession canAddInput:input]) {
[captureSession addInput:input];
} else {
NSLog(@"无法添加输入设备");
return;
}
AVCapturePhotoOutput *output = [[AVCapturePhotoOutput alloc] init];
if ([captureSession canAddOutput:output]) {
[captureSession addOutput:output];
} else {
NSLog(@"无法添加输出设备");
return;
}
[output capturePhotoWithSettings:nil delegate:self completionHandler:^(CMSampleBufferRef sampleBuffer, NSError * _Nullable error) {
if (!sampleBuffer) {
NSLog(@"%@", error);
return;
}
CIImage *image = [CIImage imageWithCVPixelBuffer:sampleBuffer];
CGImageRef cgImage = [image JPEGRepresentation:1.0 quality:0.8];
CGSize size = CGSizeMake([self.imageView frame].size);
UIImage *uiImage = [UIImage imageWithCGImage:cgImage scaledToSize:size];
[self.imageView setImage:uiImage];
}];
} else {
NSLog(@"设备不支持视频捕获");
}
}
@end
类
分组导航标记
self
NSObject
super
访问修饰限定符
property
synthesize
万能指针
instancetype
构造方法
自定义构造方法
引用计数器
僵尸对象
@class
自动释放池
ARC
mrc转arc
将一个类分为多个模块
非正式协议
延展
block
协议
NSString
NSURL
NSRange
NSMutableString
NSArray
NSDictionary
NSFileManager
CGPoint