2012-12-07 32 views
0

我只在使用以下代碼時在iPad上出現錯誤/崩潰,iPhone正常工作。UIPopoverController如何修復iPad?

它基本上是當試圖加載畫廊挑選一個圖像。

調試錯誤是; 「在iPad上,必須的UIImagePickerController通過UIPopoverController「呈現」

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (buttonIndex == 0) { 
     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
      UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
      picker.delegate = self; 
      picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
      [self presentModalViewController:picker animated:YES]; 
      [picker release]; 
     } 
     else { 
      UIAlertView *alert = [[UIAlertView alloc] 
            initWithTitle:@"Error accessing photo library" 
            message:@"Device does not support a photo library" 
            delegate:nil 
            cancelButtonTitle:@"Abort!" 
            otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     } 

    } 
     if (buttonIndex == 1) { 


     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
      UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
      picker.delegate = self; 
      picker.allowsEditing = YES; 
      picker.sourceType =UIImagePickerControllerSourceTypeCamera; 
      [self presentModalViewController:picker animated:YES]; 
      [picker release]; 
     } 
     else { 
      UIAlertView *alert = [[UIAlertView alloc] 
            initWithTitle:@"Error accessing Camera" 
            message:@"Device does not support a Camera" 
            delegate:nil 
            cancelButtonTitle:@"Abort!" 
            otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     } 

     } 


    } 


    -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
     NSLog(@"the info opf picker is %@",info); 
     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init]; 
     NSData *imageData; 

     if ([info objectForKey:UIImagePickerControllerEditedImage]) { 
      UIImage *image2=[info objectForKey:UIImagePickerControllerEditedImage]; 
      //UIImage *myscaledImage=[self scaleImage:image2 toSize:CGSizeMake(18, 24)]; 
      //imageData = UIImagePNGRepresentation(myscaledImage); 

      imageData=UIImageJPEGRepresentation(image2, .4); 
      NSLog(@"the lenght of the edited image data is %d",[imageData length]); 

     } 
     else { 
      UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage]; 

      //UIImage *myscaledImage=[self scaleImage:image toSize:CGSizeMake(18, 24)]; 
      //imageData = UIImagePNGRepresentation(myscaledImage); 

      imageData=UIImageJPEGRepresentation(image, .6); 
      NSLog(@"the lenght of the image dataa is %d",[imageData length]); 

     } 


     NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:imageData,@"Image",@"Theme",@"Cell_Text",nil]; 
    // NSLog(@"the custom array%@ and the row =%d", customArray,currentCameraRow); 
     [customArray replaceObjectAtIndex:currentCameraRow withObject:tempDict]; 




     [pool drain]; 


     //NSLog(@"") 
     [self startThread];   

     [picker dismissModalViewControllerAnimated:YES]; 
     [myCustomTableView reloadData]; 

     NSLog(@"size of myObject: %zd", malloc_size(myCustomTableView)); 

    } 

誰能這麼好心幫助上面的代碼?我正在猜測一個if/else的陳述,但沒有第一個想法是什麼以及將它放在哪裏。

由於提前,

克里斯

編輯 - 好吧,我現在有工作從畫廊負載現在加載在一個小窗口,在這裏我可以選擇「照片」,但沒有「選擇'或'選擇'按鈕,如果我點擊照片,沒有打勾,說它被選中,但它會選擇它,但要解僱我在窗外點擊......所以它可以工作,但我猜測我錯過了一些東西,這裏是代碼;

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
if (buttonIndex == 0) { 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

     if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
     { 
      if(popoverController != nil) 
      { 
       [popoverController release]; 
       popoverController = nil; 
      } 
      UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:picker]; 
      popoverController = popover; 
      popoverController.delegate = self; 
      [popover presentPopoverFromRect:CGRectMake(0, 0, 200, 800) 
            inView:self.view 
        permittedArrowDirections:UIPopoverArrowDirectionAny 
            animated:YES]; 
     } 
     else 
     { 
      [self presentModalViewController:picker animated:YES]; 
     } 



//  [self presentModalViewController:picker animated:YES]; 
     [picker release]; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle:@"Error accessing photo library" 
           message:@"Device does not support a photo library" 
           delegate:nil 
           cancelButtonTitle:@"Abort!" 
           otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 

} 
    if (buttonIndex == 1) { 


    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsEditing = YES; 
     picker.sourceType =UIImagePickerControllerSourceTypeCamera; 
     [self presentModalViewController:picker animated:YES]; 
     [picker release]; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle:@"Error accessing Camera" 
           message:@"Device does not support a Camera" 
           delegate:nil 
           cancelButtonTitle:@"Abort!" 
           otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 

    } 


} 


-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
    NSLog(@"the info opf picker is %@",info); 
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init]; 
    NSData *imageData; 

    if ([info objectForKey:UIImagePickerControllerEditedImage]) { 
     UIImage *image2=[info objectForKey:UIImagePickerControllerEditedImage]; 
     //UIImage *myscaledImage=[self scaleImage:image2 toSize:CGSizeMake(18, 24)]; 
     //imageData = UIImagePNGRepresentation(myscaledImage); 

     imageData=UIImageJPEGRepresentation(image2, .4); 
     NSLog(@"the lenght of the edited image data is %d",[imageData length]); 

    } 
    else { 
     UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage]; 

     //UIImage *myscaledImage=[self scaleImage:image toSize:CGSizeMake(18, 24)]; 
     //imageData = UIImagePNGRepresentation(myscaledImage); 

     imageData=UIImageJPEGRepresentation(image, .6); 
     NSLog(@"the lenght of the image dataa is %d",[imageData length]); 

    } 


    NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:imageData,@"Image",@"Theme",@"Cell_Text",nil]; 
// NSLog(@"the custom array%@ and the row =%d", customArray,currentCameraRow); 
    [customArray replaceObjectAtIndex:currentCameraRow withObject:tempDict]; 




    [pool drain]; 


    //NSLog(@"") 
    [self startThread];   

    [picker dismissModalViewControllerAnimated:YES]; 
    [myCustomTableView reloadData]; 

    NSLog(@"size of myObject: %zd", malloc_size(myCustomTableView)); 



} 

-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize { 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 


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

回答

0

的錯誤是很清楚的:

在iPad上,的UIImagePickerController必須通過提交UIPopoverController」

當上了運行你需要把圖像拾取的酥料餅控制器iPad兼容。當sourceType用於照片庫時,情況屬實。基於相機的圖像選擇器可以顯示爲全屏模式視圖控制器。

這不是iPhone上的問題,因爲iPhone不支持UIPopoverController

示例應用程序PrintPhoto演示瞭如何在彈出窗口中顯示圖像選擇器。具體見PrintPhotoViewController.m

+0

馬迪喜閱讀本文檔的酥料餅,我就知道大致這是解決方案,但我的問題是更多如何做到這一點,而不是爲什麼它發生:)你可以協助代碼方面呢? – user1695971

+0

@ user1695971看到我更新的答案。 – rmaddy

+0

好吧,我想我已經修復它到現在的一個點...請你可以檢查我更新的問題,因爲它只是我現在需要的一點幫助:) – user1695971

0

做這樣的事情:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
    //present as a pop over vc 
} else { 
    //present as a modal vc 
} 

,現在你可以使用這個方法

- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated 

您可以在此鏈接link

+0

好的我認爲我現在已經修復它了...請檢查一下我更新的問題,因爲它只是我現在需要的一小部分幫助: ) – user1695971

相關問題