2013-04-23 102 views
3

屏幕,我设置的AVCaptureSession预设PhotoPreset捕捉图像预览像与AVFoundation

self.session.sessionPreset = AVCaptureSessionPresetPhoto; 

,然后添加一个新层,进入我的看法与

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; 
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 
CALayer *rootLayer = [self.view layer]; 
[rootLayer setMasksToBounds:YES]; 
[previewLayer setFrame:[rootLayer bounds]]; 
[rootLayer addSublayer:previewLayer]; 

到目前为止好,但是上当我想要捕捉图像时,我使用下面的代码

AVCaptureConnection *videoConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo]; 

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) 
{ 

    [self.session stopRunning]; 

    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
    UIImage *image = [[UIImage alloc] initWithData:imageData ]; 

    self.imageView.image = image; //IMAGEVIEW IS WITH THE BOUNDS OF SELF.VIEW 
    image = nil; 


}]; 

捕获图像是f然而,与在屏幕上显示的AVCaptureVideoPreviewLayer相比,所捕获的图像是不同的。我真正想要做的就是展示抓取图案,就像出现在AVCapturePreviewLayer图层上一样。我怎样才能做到这一点?我应该如何调整和裁剪拍摄的图像相对于self.view的界限?

+0

我有同样的问题,你有没有找到解决方案或可能导致我们找到解决方案的东西? (: 谢谢。 – 2013-05-20 12:04:01

+0

WWDC 2011 Session 419解决了我所有的问题,他们也有示例代码,以解决您的问题:) – kkocabiyik 2013-05-27 22:06:22

+1

@kkocabiyik,虽然我很欣赏参考,但如果您可以详细说明你在这里解决你自己的问题。人们将继续通过谷歌或搜索找到你未答复的问题,并发表评论说,一小时长的视频解决了你的问题并不完全帮助其他人。至少时间戳会有帮助。 – bradleygriffith 2013-08-05 19:06:45

回答

-1

我不知道这会帮助你或没有,但你谈论ImagePickerView与下面的代码裁剪图像,所以我裁剪图像我不知道这会帮助你或不

- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) { 
     UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
     UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size); 
     self.imagee = shrunkenImage; 
     selectImage.image = imagee; 

    }  [picker dismissModalViewControllerAnimated:YES]; 
} 


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {  
    [picker dismissModalViewControllerAnimated:YES]; 
} 

// function for cropping images you can do some changes in parameter as per your requirements 
static UIImage *shrinkImage(UIImage *original, CGSize size) { 
    CGFloat scale = [UIScreen mainScreen].scale; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale, 
               size.height * scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst); 
    CGContextDrawImage(context, 
         CGRectMake(0, 0, size.width * scale, size.height * scale), 
         original.CGImage); 
    CGImageRef shrunken = CGBitmapContextCreateImage(context); 
    UIImage *final = [UIImage imageWithCGImage:shrunken]; 

    CGContextRelease(context); 
    CGImageRelease(shrunken); 

    return final; 
} 
+0

它实际上不是关于裁剪和调整大小。由于AVFoundation返回给我一个原始图像大小的图像,因此我需要根据在捕获图像时使用AVCaptureVideoPreviewLayer显示在屏幕上的图像裁剪并调整大小。 – kkocabiyik 2013-04-23 12:31:14

+0

对不起,我想帮助 – 2013-04-23 12:33:54

+0

eheh没问题:) – kkocabiyik 2013-04-23 12:34:16