使用UIImagePickerController检索照片库中的选定图像

时间:2012-04-23 22:55:36

标签: iphone objective-c uiimagepickercontroller photo

我使用UIImagePickerController有点问题...我想使用用户从iPhone库中选择的图像,并在实例化我的viewController时使用它:

首先,用户必须单击StartWithImage按钮:

-(IBAction)startWithImage:(UIButton *) sender
{
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:self.imgPicker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    [self dismissModalViewControllerAnimated:YES];
    [imagePicker release];
    //UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    photo =(UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
    //code for starting game
}

这是我实例化viewController的方法:

imageSliderViewController = [[ImageSliderViewController alloc] initWithSize:colMax :rowMax:photo];

我的视图控制器在viewDidLoad中使用照片,如下所示:

CGImageRef cgImg = CGImageCreateWithImageInRect(photo.CGImage, CGRectMake(ox, oy, width-1, height-1));//-1 for block effect

我收到了EXC_BAD_ACCESS错误...

1 个答案:

答案 0 :(得分:0)

好吧,EXC_BAD_ACCESS通常在对象发布时发生,我们在某处尝试访问此对象。

试试这个

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{

    //UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    photo =(UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
    //code for starting game

    // dismiss controller after working with it
    [self dismissModalViewControllerAnimated:YES];
    [imagePicker release];
}