2011-05-24 94 views
2

我試過尋找一個解決這個問題的答案,但找不到任何有用的信息。如何在編輯時將圖像疊加添加到UIImagePickerControllerSourceTypePhotoLibrary?

基本上,我希望用戶從相冊中選擇一張圖片,然後讓該應用在編輯/剪裁模式下顯示圖片。

在此編輯屏幕上,我想顯示一個覆蓋屏幕供用戶用來對齊他的圖像。

然後他點擊保存按鈕,通過合併圖像來保存編輯後的圖片與覆蓋圖像。

我無法找到的唯一辦法是在編輯屏幕中添加疊加圖像!

,如果我能做到這一點很容易:

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

但當:

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

我不能使用:

imagePicker.cameraOverlayView = overlayGraphicView;

這就是我的問題。

另外,如果我將UIimage添加爲子視圖,而不是此疊加層,即使用戶從相冊中選擇他的圖像,它仍會保留在屏幕上。我只想要在選擇圖像進行編輯之後以及在編輯完成後將其保存到相冊之前出現疊加層。

感謝您的幫助。

繼承人的方法:

- (IBAction)pickPhotoButton:(id)sender { 
    NSLog(@"Pick a Current Photo Button Pressed..."); 

    // Create image picker controller 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 

    // Set source to the camera 
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

    // Delegate is self 
    imagePicker.delegate = self; 

    // Allow editing of image ? 
    imagePicker.allowsEditing = YES; 

    // Show image picker 
    [self presentModalViewController:imagePicker animated:YES]; 
} 

和我的繼承人保存到相冊的方法:

// Save the image to photo album 
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{  
    // Access the uncropped image from info dictionary 
    UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; 

    // Combine image with overlay before saving!! 
    image = [self addOverlayToImage:image]; 

    // Save image 
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 

    [picker release]; 
} 

回答

6

你可以成爲UIImagePickerController對象的委託,並敏銳地觀察UINavigationControllerDelegate方法 - navigationController:didShowViewController:animated:navigationController:willShowViewController:animated:。你可以知道第二個視圖控制器何時開始,然後將你的覆蓋圖添加到視圖控制器的視圖中。用法示例如下 -

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
    if ([navigationController.viewControllers count] == 2) { 
     overlayView.center = CGPointMake(135, 135); 
     overlayView.bounds = CGRectZero; 

     [viewController.view addSubview:overlayView]; 

     [UIView beginAnimations:@"animateTableView" context:nil]; 
     [UIView setAnimationDuration:0.4]; 
     [overlayView setFrame:CGRectMake(50, 50, 220, 220)]; 
     [UIView commitAnimations]; 
    } 
} 
+0

哇非常感謝迪帕克,完美地工作!從來沒有能夠發現我自己,所以幫助絕對不勝感激!您是否還有機會知道如何拍攝全屏的剪輯圖像(裁剪後),而不是剪切框中的圖像?我不想截圖,因爲這也會顯示酒吧等... – GameDev 2011-05-25 08:36:00

+0

我沒有得到你在這一個。 – 2011-05-25 09:46:37

+0

基本上,當你從庫中選擇一個圖像進入編輯屏幕,在這個屏幕上是一個正方形的裁剪框,我可以在這裏編輯圖像(平移和縮放),然後將圖像保存到圖庫中通過點擊下一步。我想知道的是,我可以如何保存整個縮放圖像,而不僅僅是裁剪框中的圖像?謝謝 – GameDev 2011-05-25 11:33:32

0

您可以使用下面的定製方法將覆蓋到Imagepickercontroller

在yourclassname .h文件中定義

@property (nonatomic, retain) UIImagePickerController*  m_Pickercontroller; 
@property(nonatomic,retain) UIView *cameraOverlayView; 


In yourclassname.m file 

-(void)Loadcaptureview 
{ 
    //Allocating Image Picker Controller 
    m_Pickercontroller=[[UIImagePickerController alloc] init ]; 
    m_Pickercontroller.delegate=self; 
    m_Pickercontroller.allowsEditing=YES; 
    m_Pickercontroller.sourceType=UIImagePickerControllerSourceTypeCamera; 
    m_Pickercontroller.view.frame=CGRectMake(0, 0, 320, 300); 

    UIButton*Clickbutton=[[UIButton alloc] initWithFrame:CGRectMake(50,200, 60, 60)]; 
    [Clickbutton setImage:[UIImage imageNamed:@"red-circle-button.png"] forState:UIControlStateNormal]; 
    [Clickbutton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside]; 

    image1=[[UIImageView alloc] initWithFrame:CGRectMake(50,400, 60, 60)]; 
    image1.image=[UIImage imageNamed:@"red-circle-button.png"]; 

    [m_Pickercontroller.cameraOverlayView addSubview:Clickbutton]; 
    [m_Pickercontroller.cameraOverlayView addSubview:image1]; 
    [m_Pickercontroller.cameraOverlayView bringSubviewToFront:image1]; 

    //Hiding the Camera Control 
    m_Pickercontroller.showsCameraControls=NO; 

    //Presenting the imagepicker Controller to Present Modal view Controller 
    [self.navigationController presentModalViewController:m_Pickercontroller animated:YES]; 
    } 

    // Calling the Function 

    - (void)viewDidLoad 
    { 

    [self Loadcaptureview]; 

    }