
我的代码如下:
- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; // Do any additional setup after loading the vIEw. session = [[AVCaptureSession alloc] init]; session.sessionPreset = AVCaptureSessionPresetMedium; captureVIDeoPrevIEwLayer = [[AVCaptureVIDeoPrevIEwLayer alloc] initWithSession:session]; captureVIDeoPrevIEwLayer.vIDeoGravity = AVLayerVIDeoGravityResizeAspectFill; captureVIDeoPrevIEwLayer.frame = vImagePrevIEw.bounds; [vImagePrevIEw.layer addSublayer:captureVIDeoPrevIEwLayer]; AVCaptureDevice *device = [self backFacingCameraifAvailable]; 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]; 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; } } [session startRunning]; [session addOutput:stillimageOutput];}-(AVCaptureDevice *)backFacingCameraifAvailable{ NSArray *vIDeoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVIDeo]; AVCaptureDevice *captureDevice = nil; for (AVCaptureDevice *device in vIDeoDevices){ if (device.position == AVCaptureDevicepositionBack){ captureDevice = device; break; } } // Couldn't find one on the back,so just get the default vIDeo device. if (!captureDevice){ captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVIDeo]; } return captureDevice;} 以下是捕获图像的代码:
- (IBAction)captureTask { 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]; stillimage = image; }];} 我面临的问题是,它正在拍摄照片,并保存到stillimage,但是,从我所知道的,该图像适用于整个iPhone屏幕.这不是我创建的UIVIEw * vImagePrevIEw的范围.有没有办法剪辑捕获图像的边界?
[编辑]
阅读文档后,我意识到图像是正确的分辨率,如下所示:session.sessionPreset = AVCaptureSessionPresetMedium;.有没有办法让图像像一个正方形?就像Instagram制作图像一样?根据文档的所有会话预设都不是所有正方形:(
我试过以下:
captureVIDeoPrevIEwLayer.vIDeoGravity = AVLayerVIDeoGravityResize;
但是,它仅调整图像大小以适合当前视图,不会生成方形图像.
解决方法 我理解你的沮丧,预设应该是可定制的或有更多的选择!我对我的图像所做的就是围绕中心裁剪它们,我为此编写了以下代码:- (UIImage *)crop:(UIImage *)image from:(CGSize)src to:(CGSize)dst{ CGPoint cropCenter = CGPointMake((src.wIDth/2),(src.height/2)); CGPoint cropStart = CGPointMake((cropCenter.x - (dst.wIDth/2)),(cropCenter.y - (dst.height/2))); CGRect cropRect = CGRectMake(cropStart.x,cropStart.y,dst.wIDth,dst.height); CGImageRef cropRef = CGImageCreateWithImageInRect(image.CGImage,cropRect); UIImage* cropImage = [UIImage imageWithCGImage:cropRef]; CGImageRelease(cropRef); return cropImage;} 其中src表示原始尺寸,dst表示裁剪尺寸;和图像当然是你想要裁剪的图像.
总结以上是内存溢出为你收集整理的ios – 使用边界捕获图像?全部内容,希望文章能够帮你解决ios – 使用边界捕获图像?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)