回答
这里是一步一步的教程,如何使用AVFoundation
捕捉图像并将其保存到相册。
添加UIView
对象笔尖(或作为一个子视图),并创建你的控制器@property
:
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
的UIView
连接到插座上面IB,或直接,如果你”为其分配重新使用代码而不是NIB。
然后编辑您的UIViewController
,并给它下面viewDidAppear
方法:
-(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];
}
创建一个新的@property
抱到输出对象的引用:
@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
然后做一个UIImageView
我们”将显示拍摄的照片。将其添加到您的NIB,或以编程方式。
挂钩到另一个@property
,或手动分配它,例如;
@property(nonatomic, retain) IBOutlet UIImageView *vImage;
最后,创建一个UIButton
,以便拍摄照片。
再次,将其添加到您的NIB(或程序到你的屏幕),并把它挂到下面的方法:
-(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);
}];
}
您可能必须进口#import <ImageIO/CGImageProperties.h>
也。
以这种方式设置相机的方法很多,您可能没有做或没有显示。
最好看的地方是:在AVCam示例项目中的AVCamCaptureManager.m
;特别是setupSession
和captureStillImage
(将照片写入库)。
根据你的问题,看起来你已经从相机的图片到NSData或UIImage。如果是这样,您可以以不同的方式将此图片添加到相册中。 AVFoundation本身没有持有图像的类。 因此,例如,您可以使用ALAssetsLibrary框架将图像保存到相册中。 或者你可以使用UIKit框架,它的方法是UIImageWriteToSavedPhotosAlbum。两者都很好用。
如果您还没有捕获静止图像,可以查看AVFoundation框架的captureStillImageAsynchronouslyFromConnection方法。无论如何,这里有一些想法。你可以很容易地在互联网上找到例子。 祝你好运:)
此方法适用于我
-(void) saveImageDataToLibrary:(UIImage *) image
{
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[appDelegate.library writeImageDataToSavedPhotosAlbum:imageData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error) {
NSLog(@"%@",error.description);
}
else {
NSLog(@"Photo saved successful");
}
}];
}
其中appDelegate.library是ALAssetLibrary
实例。
谢谢,但是您是如何使用AVFoundation从前置摄像头设备提取UIImage的? – RollRoll
- 1. 拍摄照片并保存在SD卡或相机相册PHONEGAP
- 2. 如何保存拍摄的照片
- 3. 拍摄照片,不保存
- 4. 拍摄照片不保存
- 5. 如何创建拍摄照片的功能拍摄+保存
- 6. 拍摄照片并保存到/ data/data
- 7. Phonegap相机API保存拍摄到相机胶卷的照片
- 8. AVFoundation每秒拍摄一张照片SWIFT
- 9. 保存照片到相册
- 10. 保存视频到相机拍摄的照片库
- 11. C#保存拍摄的照片
- 12. 拍照,并在保存到相册
- 13. Android:使用相机意图拍摄照片并将其保存到java.io.File
- 14. 从相机拍摄照片
- 15. 使用UIViewController(MonoTouch)相机拍摄照片
- 16. Monodroid使用相机拍摄照片
- 17. Android相机拍摄照片时不保存到SD卡
- 18. 如何使用拍摄的照片?
- 19. 相机拍摄照片在Android临时保存图片?
- 20. Android:将拍摄的照片保存到Imagefolder
- 21. 如何将照相机拍摄的照片存储在内存中?
- 22. 使用CvVideoCamera拍摄照片
- 23. 使用QTCaptureView拍摄照片
- 24. 将照片保存到iPhone相册中的自定义相册
- 25. Android - 从相机拍摄照片并将其保存到内部存储
- 26. 如何在应用程序iOS中保存拍摄的照片?
- 27. 避免画廊保存在Android的相机拍摄的照片
- 28. android中的java.lang.OutOfMemoryError保存从相机拍摄的照片
- 29. 关于相机方向的变化,照片未拍摄/保存
- 30. Windows Phone 7.1 - 保存相机拍摄的照片路径CapsureTask
我在做,谢谢 – RollRoll
这个主题似乎并没有涉及可可。 –