在ipad上的图像选择器

时间:2012-11-30 01:36:50

标签: objective-c xcode ipad uiimagepickercontroller

我知道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];
}

2 个答案:

答案 0 :(得分:1)

根据您获得的例外情况,问题是sender方法的addPhoto实际上是UIBarButtonItem,而不是UIButton。您需要更改弹出窗口的显示方式,使用按钮的框架从条形按钮显示。

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

    [popover presentPopoverFromBarButtonItem:sender
        permittedArrowDirections:UIPopoverArrowDirectionAny
        animated:YES];
}

答案 1 :(得分:0)

尝试创建属性以存储您的popover控制器或存储内容控制器。似乎一个视图控制器在方法结束后立即发布。此外,iOS 7在弹出窗口中具有UIImagePickerController的可怕行为。 This is related question.