2012-06-13 43 views
0

挑選圖片的時候我是比較新的X代碼 和 餘米工作的照片拼貼應用程序,當我拿起圖像從一個選擇器,然後圖像選擇器工作正常,但我想從不同的圖像挑選不同的圖像,然後圖像選擇器不能正常工作如何調用imagepickerdelegate方法兩次從dirrerent按鈕

任何人都可以幫助我解決我的問題。這裏是我的代碼

`

-(IBAction)imagepickMethod1:(id)sender 
{ 
    imagepicker=[[UIImagePickerController alloc]init]; 
    imagepicker.delegate=self; 
    imagepicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentModalViewController:imagepicker animated:YES]; 
    button1.tag=100; 
} 
-(IBAction)imagepickMethod2:(id)sender 
{ 
    imagepicker1=[[UIImagePickerController alloc]init]; 
    imagepicker1.delegate=self; 
    imagepicker1.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentModalViewController:imagepicker1 animated:YES]; 


} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{ 
    [picker dismissModalViewControllerAnimated:YES]; 
    imagepicker.view.hidden=YES; 


     photoPreviewImageView.image=image; 


} 
-(void)imagePickerController1:(UIImagePickerController *)picker1 didFinishPickingImage:(UIImage *)image1 editingInfo:(NSDictionary *)editingInfo1 
{ 
    [picker1 dismissModalViewControllerAnimated:YES]; 
    imagepicker.view.hidden=YES; 
      photoPreviewImageView1.image=image1; 

} 

`

+1

請你的問題更清楚,所以,我們可以幫你 – TheTiger

回答

1
//Take two imageView in your .h file 
UIImageView *imgViewForFirstPicker; 
UIImageView *imgViewForSecondPicker; 

// Alloc these images in view did load 

imgViewForFirstPicker = [[UIImaeView allo] initWithFrame:(give your rect)]; 

// Similarly for second imageView and add to both in self.view 

-(IBAction)imagepickMethod1:(id)sender 
{ 
    UIImagePickerController *imagepicker=[[UIImagePickerController alloc]init]; 
    imagepicker.delegate=self; 

    imagepicker.tag=100; 
    imagepicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentModalViewController:imagepicker animated:YES]; 
} 
-(IBAction)imagepickMethod2:(id)sender 
{ 
    UIImagePickerController *imagepicker1=[[UIImagePickerController alloc]init]; 
    imagepicker1.delegate=self; 
    imagepicker1.tag=101; 
    imagepicker1.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentModalViewController:imagepicker1 animated:YES]; 
} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage*)image editingInfo:(NSDictionary *)editingInfo 
{ 
    [picker dismissModalViewControllerAnimated:YES]; 
    if(picker.tag == 100) 
    imgViewForFirstPicker.image=image; 
    else 
    imgViewForSecondPicker.image=image; 
} 

試試這個希望它會幫助你

+0

感謝阿布舍克我們的代碼工作正常 –

+0

但在我的應用程序imagepicker.tag不工作。 。你能告訴我我爲這個問題做什麼 –

+0

而不是imagepicker.tag只是創建一個公共的字符串變量使用它.. –

0

UIImagePickerController創建了一個相當沉重的對象,所以我不認爲這是明智的創建它的多個實例。可能是這是什麼導致的問題,如果你這樣做..這將是很好,如果你可以分享你的代碼以及我們可以有更多的見解到你的問題

0

建議只使用圖像選擇器。從你提供的代碼試圖創建兩個不同的imagepicker委託方法,但實際上只有其中一個將被稱爲兩次。

您應該在imagePicker的實例上創建並根據您需要更改的圖像更改其標籤,然後在-didFinishPickingImage檢查if (picker.tag == SOME_TAG)中進行適當設置。