0

仅当使用以下代码时,我在 iPad 上遇到错误/崩溃,iPhone 工作正常。

基本上是在尝试加载图库以选择图像时。

调试错误是;“在 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];
}
4

2 回答 2

0

错误很明显:

在 iPad 上,UIImagePickerController 必须通过 UIPopoverController 呈现

在 iPad 上运行时,您需要将图像选择器放在弹出框控制器中。sourceType当用于照片库时,这是正确的。基于相机的图像选择器可以显示为全屏模式视图控制器。

这在 iPhone 上不是问题,因为 iPhone 不支持UIPopoverController.

示例应用程序PrintPhoto演示了如何在弹出窗口中显示图像选择器。具体见PrintPhotoViewController.m

于 2012-12-07T18:08:45.217 回答
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

您可以在此链接链接中阅读文档

于 2012-12-07T18:19:07.177 回答