2010-08-21 101 views
0

我嘗試使用下面的代碼來獲取屏幕快照從我的UIImagePickerControlleriPhone - UIImagePickerController採取屏幕截圖?

UIGraphicsBeginImageContext(imagePicker.view.bounds.size); 
[self.imagePicker.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

它顯示了一切,除了什麼是從相機拍攝,我看到了取消按鈕,拍照BUTTOM。 而不是在相機上顯示它捕捉黑屏,有沒有辦法解決這個問題?或者更好的方法每隔1秒自動從相機捕捉圖像?

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection 
{ 
    UIImage *image = [self imageFromSampleBuffer:sampleBuffer]; 
    UIImageView *img = [[UIImageView alloc] initWithImage:image]; 
    [img setFrame:CGRectMake(0, 0, 320, 480)]; 
    [self.view addSubview:img]; 
    [img release]; 
} 

回答

1

您想使用新的(在OS 4中)AVFoundation API。

試試這個頁面的一些信息:

http://developer.apple.com/iphone/library/qa/qa2010/qa1702.html

+0

感謝,這似乎是最好的辦法,但遠遠Ø我接收到的數據無法轉換到一個UIImage。我會更新 – aryaxt 2010-08-21 23:53:43

+0

我發佈的鏈接中的示例代碼應該有所幫助。部分向下是一行,它說<使用圖像在這裏添加您的代碼>。在這一點上,你有一個喜歡做你喜歡的事情。 – 2010-08-21 23:59:28

+0

是的一切工作正常,我收到captureOutput,但是當我將它轉換爲uiimage並創建一個imageview並將其添加到我的觀點,uiimageview不顯示圖片,我想不知何故數據被中斷。這是否設想在iphone 3g上工作?因爲iphone 3g不允許錄像。 – aryaxt 2010-08-22 00:23:46