2014-02-06 206 views
1

我正面臨一些真正的問題。我已經學習了幾個使用UIImagePickerController和AVFoundation在相機中添加疊加層的教程。一切工作都很好。我可以使用兩者在相機上添加疊加層。然而,我主要關心的是,當用戶點擊掃描按鈕時,提取該覆蓋視圖下的那部分圖像。我在覆蓋圖和按鈕中顯示白色視圖,因此只能檢索該白色視圖下的圖像。iOS相機覆蓋拍攝圖像

-(IBAction)cameraClicked:(id)sender { 

    OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    self.imagePickerController = [[UIImagePickerController alloc] init]; 
    self.imagePickerController.delegate = self; 

    if ([UIImagePickerController 
    isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) { 
     self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
     self.imagePickerController.cameraCaptureMode =  
     UIImagePickerControllerCameraCaptureModePhoto; 
    } 
    else { 
     self.imagePickerController.sourceType =  
     UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
    } 

    self.imagePickerController.showsCameraControls = NO; 
    self.imagePickerController.navigationBarHidden = YES;  
    self.imagePickerController.toolbarHidden = YES; 

    self.imagePickerController.wantsFullScreenLayout = YES; 
    self.imagePickerController.cameraViewTransform =  
    CGAffineTransformScale(self.imagePickerController.cameraViewTransform, 
    CAMERA_TRANSFORM_X,CAMERA_TRANSFORM_Y); 
    self.imagePickerController.cameraOverlayView = overlay; 
    [self presentViewController:self.imagePickerController animated:YES  
    completion:nil]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    CGRect cropRect; 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    UIImage *crosshair = [UIImage imageNamed:@"overlay.png"]; 

    UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)); 
    [image drawInRect: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    if(isIOS7) 
     cropRect = CGRectMake(10, 440, crosshair.size.width, crosshair.size.height); 
    else 
     cropRect = CGRectMake(10, 405, crosshair.size.width, crosshair.size.height); 

    CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect); 
    UIImage *setImage = [UIImage imageWithCGImage:imageRef]; 
    self.finalImageView.image = setImage; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

我已經更新了我的問題與代碼 – user2342321

回答

2

嘗試......它的工作..

self.imagePickerController=nil; 
    self.imagePickerController = [[UIImagePickerController alloc] init]; 
    self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
    self.imagePickerController.delegate = self; 
    self.imagePickerController.allowsEditing = NO; 
    self.imagePickerController.showsCameraControls = NO; 

//創建視圖疊加

CGRect overlayRect = CGRectMake(0, 0, self.imagePickerController.view.frame.size.width,  self.imagePickerController.view.frame.size.height); 

    UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect]; 

    btn_Overlay = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn_Overlay setBackgroundImage:[UIImage imageNamed:@"IMageNmae"] forState:UIControlStateNormal]; 
    btn_Overlay.frame = CGRectMake(80.0, 170, 150.0, 30.0); 
    btn_Overlay.tag = 0; 
    [btn_Overlay setTitle:@"Button Title" forState:UIControlStateNormal]; 
    [btn_Overlay addTarget:self action:@selector(ActinMethod:) forControlEvents:UIControlEventTouchUpInside]; 
    [overlayView addSubview:btn_Overlay]; 

[self.imagePickerController setCameraOverlayView:overlayView]; 
    // display imagePicker 
    [self.navigationController presentViewController:self.imagePickerController animated:YES completion:nil]; 
+0

感謝您的回答,我有做到這一點,但是當我點擊按鈕操作方法時,我需要提取疊加視圖中顯示在相機中的圖像。 – user2342321

+0

你可以提取iamge(ActinMethod)。在Action方法中寫代碼[self.imagePickerController takePicture]; –