2013-03-29 53 views
1

我是使用UIImagePicker的新品牌,因此請在這裏與我聯繫。基本上,我有兩個UIButton定義訪問相機膠捲,一旦圖像被選中,圖像出現在我的UIImageView中。問題是當我從其中一個按鈕選擇圖像時,它出現在兩個UIImageView中,而不是一個。我基本上希望能夠通過兩個不同的按鈕來選擇兩張不同的照片。這裏是編碼:使用UIImagePicker的多個UIImageViews

-(IBAction)getPhoto1:(id)sender { 

    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
    popover1 = [[UIPopoverController alloc] initWithContentViewController:picker]; 
    [popover1 presentPopoverFromRect:CGRectMake(0.0, 0.0, 400., 400.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 

} 

-(IBAction)getPhoto2:(id)sender { 

    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
    popover2 = [[UIPopoverController alloc] initWithContentViewController:picker]; 
    [popover2 presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 

} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    [picker dismissViewControllerAnimated:YES completion:nil]; 
    photo1.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    photo2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

} 

回答

2

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info在您完成選擇圖像時被調用。

當您完成從UIImagePickerController選擇圖像時,您將UIImageView設置爲返回的圖像。

您需要區分哪個按鈕被按下 - 也許您可以使用int來確定哪個按鈕被按下。

例如,創建一個「廉政」叫buttonPressed當按下getPhoto1那會被設置爲1,或2當按下getPhoto2。然後,在didFinishPickingMediaWithInfo中,您可以有:

if (buttonPressed == 1) { 
    photo1.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
} else if (buttonPressed == 2) { 
    photo2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
} 
+0

謝謝,這最後工作得很好。我在動作上面設置了一個int:int buttonSelect = 0;然後在不同的動作中將其設置爲1和2,並且效果很好。謝謝! – Taylor

+0

很酷,很高興它爲你工作! – rog

相關問題