0

我知道 iPad 上的图像选择器需要是一个弹出框,我一直在尝试,但是当我点击调用弹出框的按钮时它不会工作并崩溃。

   -(IBAction)addPhoto:(id)sender{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    popover=[[UIPopoverController alloc]
             initWithContentViewController:imagePicker];
    [popover presentPopoverFromRect:((UIButton *)sender).frame
                             inView:self.view
           permittedArrowDirections:UIPopoverArrowDirectionAny
                           animated:YES];
}


    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {

    // Delete any existing image.
    NSManagedObject *oldImage = imageClass.image;
    if (oldImage != nil) {
        [imageClass.managedObjectContext deleteObject:oldImage];
    }

    // Create an image object for the new image.
    NSManagedObject *myImage = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:imageClass.managedObjectContext];
    imageClass.image = myImage;

    // Set the image for the image managed object.
    [image setValue:selectedImage forKey:@"image"]; 
    [self dismissViewControllerAnimated:YES completion:nil];
}
4

2 回答 2

1

根据您得到的异常,问题在于senderforaddPhoto方法实际上是 a UIBarButtonItem,而不是 a UIButton。您需要将弹出框的显示方式从使用按钮的框架更改为从栏按钮显示。

-(IBAction)addPhoto:(UIBarButtonItem *)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    popover = [[UIPopoverController alloc]
         initWithContentViewController:imagePicker];

    [popover presentPopoverFromBarButtonItem:sender
        permittedArrowDirections:UIPopoverArrowDirectionAny
        animated:YES];
}
于 2012-11-30T03:19:34.513 回答
0

尝试创建属性来存储您的弹出框控制器或存储内容控制器。似乎其中一个视图控制器在方法结束后立即释放。此外,iOS 7 在弹出窗口中 UIImagePickerController 的行为很糟糕。 这是相关的问题。

于 2013-09-26T15:45:29.383 回答