1

我在使用操作表中的 ios 相机时遇到尴尬的问题:当用户触摸“图片按钮”时,操作表会显示两个选项(使用照片库中的照片或使用相机拍照) .

无论我选择什么选项,都不会发生任何事情,但是当我再次选择媒体类型时,它就会起作用。

贝娄是我的代码:

 - (IBAction)selectMediaType: (id)sender {
    [appDelegate hideTabBar];
    UIActionSheet *action = [[UIActionSheet alloc] 
                             initWithTitle: nil
                             delegate:self 
                             cancelButtonTitle:@"Fechar" 
                             destructiveButtonTitle: nil 
                             otherButtonTitles:@"Galeria", @"Tirar Foto", nil];

    [action showFromTabBar: appDelegate.tabController.tabBar];
}


- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        NSArray *mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];

        imagePicker.delegate = self;
        imagePicker.hidesBottomBarWhenPushed = YES;
        imagePicker.mediaTypes = mediaTypes;
        imagePicker.allowsEditing = NO;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentModalViewController:imagePicker animated:YES];
        imagePicker.hidesBottomBarWhenPushed = YES;
    } else if (buttonIndex == 1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            NSArray *mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];

            imagePicker.delegate = self;
            imagePicker.mediaTypes = mediaTypes;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

            [self presentModalViewController:imagePicker animated:YES];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] 
                                  initWithTitle:@"" 
                                  message:@"Your device does not support this feature!" 
                                  delegate:self 
                                  cancelButtonTitle:@"OK" 
                                  otherButtonTitles: nil];
            [alert show];
            [alert release];

        }
    } else {
        [appDelegate showTabBar];
    }
}



- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image;
    NSURL *mediaURL;

    mediaURL = (NSURL *) [info valueForKey:UIImagePickerControllerMediaURL];

    if (mediaURL == nil) {
        image = (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];
    }

    imageView.image = image;

    [picker dismissModalViewControllerAnimated:YES];

    [appDelegate showTabBar];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissModalViewControllerAnimated:YES];

    [appDelegate showTabBar];
}

谁能帮我解决这个问题?

谢谢。

4

1 回答 1

1

我知道了。

基本上这是一个内存泄漏问题:我正在切换视图,在超级视图中添加子视图(不是在当前视图中),但我没有从超级视图中删除旧视图,所以我遇到了内存泄漏;)

于 2012-04-19T21:29:53.327 回答