2012-06-25 53 views
-3

我正在實施imagePicker演示,但它不工作。 當我點擊選擇圖像按鈕時,什麼都不會發生。我的代碼如下:Iphone:圖像選擇器不工作

-(IBAction)pick:(id)sender 
{ 
    ipc=[[UIImagePickerController alloc]init]; 
    ipc.delegate=self; 
    ipc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 
    NSLog(@" %@",ipc.sourceType); 

    [ipc presentModalViewController:self animated:YES]; 

} 

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [[picker parentViewController]dismissModalViewControllerAnimated:YES]; 
    [picker release]; 
} 

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

    [[picker parentViewController]dismissModalViewControllerAnimated:YES]; 
    [picker release]; 
} 

回答

1

請改變我們的代碼

[ipc presentModalViewController:self animated:YES]; 

[self presentModalViewController:ipc animated:YES]; 
1

問題是你想表達的使用模式的看法:

 
[ipc presentModalViewController:self animated:YES]; 

而應該使用:

 
[self presentModalViewController:ipc animated:YES]; 
+0

由於其工作 – piyush

0

如何:

-(IBAction)pick:(id)sender 
{ 
     if ([UIImagePickerController isSourceTypeAvailable: 
      UIImagePickerControllerSourceTypeCamera]) 
     { 
      UIImagePickerController *imagePicker = 
      [[UIImagePickerController alloc] init]; 
      imagePicker.delegate = self; 
      imagePicker.sourceType = 
      UIImagePickerControllerSourceTypeCamera; 
      imagePicker.mediaTypes = [NSArray arrayWithObjects: 
             (NSString *) kUTTypeImage, 
             nil]; 
      imagePicker.allowsEditing = NO; 
      [self presentModalViewController:imagePicker 
            animated:YES]; 

      newMedia = YES; 
     } 


-(void) imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self.popoverController dismissPopoverAnimated:YES]; 
    NSString *mediaType = [info 
          objectForKey:UIImagePickerControllerMediaType]; 

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { 
     UIImage *image = [info 
          objectForKey:UIImagePickerControllerOriginalImage]; 

     imageView.image = image; 
     if (newMedia) 
      UIImageWriteToSavedPhotosAlbum(image, 
              self, 
              @selector(image:finishedSavingWithError:contextInfo:), 
              nil); 
    } 
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) 
    { 
     // Code here to support video if enabled 
    } 
}