Đây là hướng dẫn từng bước về cách chụp ảnh bằng cách sử dụng AVFoundation
và lưu nó vào album ảnh.
Thêm một đối tượng UIView
đến NIB (hoặc như là một subview), và tạo ra một @property
trong điều khiển của bạn:
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
Nối UIView
vào ổ cắm trên trong IB, hoặc gán nó trực tiếp nếu bạn' tái sử dụng mã thay vì một NIB.
Sau đó chỉnh sửa UIViewController
của bạn, và cung cấp cho nó các viewDidAppear
phương pháp sau đây:
-(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];
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];
}
Tạo mới @property
để giữ một tham chiếu đến đối tượng đầu ra:
@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
Sau đó tạo một UIImageView
nơi chúng tôi sẽ hiển thị ảnh đã chụp. Thêm cái này vào NIB của bạn, hoặc lập trình.
Treo nó lên một số khác @property
hoặc gán thủ công, ví dụ :;
@property(nonatomic, retain) IBOutlet UIImageView *vImage;
Cuối cùng, hãy tạo UIButton
để bạn có thể chụp ảnh.
Một lần nữa, thêm nó vào NIB của bạn (hoặc lập trình để màn hình của bạn), và móc nó lên đến phương pháp sau:
-(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;
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}];
}
Bạn có thể phải nhập khẩu #import <ImageIO/CGImageProperties.h>
cũng có.
Source. Ngoài ra, hãy kiểm tra this.
Tôi đang làm điều đó, cảm ơn – RollRoll
Dường như chủ đề này không liên quan đến Cocoa. –