2013-04-23 98 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