简体   繁体   English

UIImagePickerController导致iOS5崩溃

[英]UIImagePickerController causes crash on ios5

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.

UINavigationController *HomeNav = [[UINavigationController alloc] initWithRootViewController:[[HomeController alloc] init]];

[HomeNav.navigationBar setTintColor:[UIColor clearColor]];

CustomTabBar *tabBar = [[CustomTabBar alloc] init];
tabBar.buttonImages = [NSArray arrayWithObjects:@"t1.png" , nil];
tabBar.hightLightButtonImages = [NSArray arrayWithObjects:@"th1.png",  nil];
tabBar.viewControllers = [NSArray arrayWithObjects:HomeNav , nil];
self.tabBarController = tabBar;
[tabBar release];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

return YES;
}

in HomeController view, there is one button if tap the button I call `AViewController. HomeController视图中,如果点击我称为`AViewController的按钮,则会有一个按钮。

-(IBAction)tapButtonA:(id)sender;
{
    [self.navigationController pushViewController:AViewController animated:YES];
}

there is also a button on the view of AViewController . AViewController的视图上还有一个按钮。

if I tap the button, I call UIImagePickercontroller 如果我点击按钮,我会调用UIImagePickercontroller

-(IBAction)tapButtonB:(id)sender;
{
 UIImagePickerController *picker=[[UIImagePickerController alloc]init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;

[self presentModalViewController: picker animated:NO]; 
}

If I tap the cancel button of the UIImagePickerController 如果我点击UIImagePickerController的取消按钮

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

the UIImagePicker will dismiss, but after 1,2 seconds the app crashes and displays UIImagePicker将关闭,但是在1,2秒后,应用程序崩溃并显示

在此处输入图片说明

Welcome any comment 欢迎任何评论

Following code is solve your problem, 以下代码可以解决您的问题,

#pragma mark - UIActionsheet Delegate method
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {


                self.imagePicker = [[UIImagePickerController alloc] init];
                self.imagePicker.delegate = self;
                self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                self.imagePicker.allowsEditing = NO;
                self.imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
                [self presentModalViewController:self.imagePicker animated:YES];
            }
            else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                                message:@"Unable to connect camera." 
                                                               delegate:self cancelButtonTitle:@"Ok" 
                                                      otherButtonTitles:nil];
                [alert show];
            }
            break;
        case 1:

            self.imagePicker = [[UIImagePickerController alloc] init];
            self.imagePicker.delegate = self;
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            self.imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
            [self presentModalViewController:self.imagePicker animated:YES];


            break;
        default:
            break;
    }
}




#pragma mark - UIImagePicker Delegate method
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];


   self.imgForEvent=image;

    //    // Save image
    //     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    //         UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    //     }
    [self dismissModalViewControllerAnimated:YES];

}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;

    // Unable to save the image  
    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                           message:@"Unable to save image to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                           message:@"Image saved to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];
    [alert show];

    [self.imagePicker dismissModalViewControllerAnimated:YES];

}

This code may helping to solveing your problem 此代码可能有助于解决您的问题

I think you want: 我想你要:

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

try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM