2011-04-11 66 views
0

在此先感謝。 我想將圖像視圖作爲覆蓋視圖添加到相機,並將(cameraview和圖像視圖)保存爲單個圖像。我搜索了它,並嘗試了在stackoverflow中給出的例子,但它沒有顯示任何東西比空白屏幕。如果任何人知道請幫助我。如何添加覆蓋視圖到cameraview並保存

+0

通過點擊對應於幫你 – visakh7 2011-04-11 09:50:58

回答

1
- (void)renderView:(UIView*)view inContext:(CGContextRef)context 
{ 
    // -renderInContext: renders in the coordinate space of the layer, 
    // so we must first apply the layer's geometry to the graphics context 
    CGContextSaveGState(context); 
    // Center the context around the window's anchor point 
    CGContextTranslateCTM(context, [view center].x, [view center].y); 
    // Apply the window's transform about the anchor point 
    CGContextConcatCTM(context, [view transform]); 
    // Offset by the portion of the bounds left of and above the anchor point 
    CGContextTranslateCTM(context, 
          -[view bounds].size.width * [[view layer] anchorPoint].x, 
          -[view bounds].size.height * [[view layer] anchorPoint].y); 

    // Render the layer hierarchy to the current context 
    [[view layer] renderInContext:context]; 

    // Restore the context 
    CGContextRestoreGState(context); 
} 

// this get called when an image has been taken from the camera 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    //cameraImage = image; 
    CGSize imageSize = [[UIScreen mainScreen] bounds].size; 
    if (NULL != UIGraphicsBeginImageContextWithOptions) 
     UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 
    else 
     UIGraphicsBeginImageContext(imageSize); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 


    // Draw the image returned by the camera sample buffer into the context. 
    // Draw it into the same sized rectangle as the view that is displayed on the screen. 
    float menubarUIOffset = 44.0; 
    UIGraphicsPushContext(context); 
    [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height-menubarUIOffset)]; 
    UIGraphicsPopContext(); 

    // Render the camera overlay view into the graphic context that we created above. 
    [self renderView:self.imagePicker.view inContext:context]; 

    // Retrieve the screenshot image containing both the camera content and the overlay view 
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil); 
    UIGraphicsEndImageContext(); 
    [self.imagePicker dismissModalViewControllerAnimated:YES]; 

} 

希望這有助於

+0

@ 7kv7我沒有用相機選項高達現在的答案刻度線只要接受一些答案,我會嘗試。感謝ü爲你的迴應。 – 2011-04-11 09:10:02

+0

你可以嘗試使用UIImagePickerController。這將是一個更容易 – visakh7 2011-04-11 09:13:53

+0

雅我只使用它。 – 2011-04-11 09:29:50

相關問題