views:

278

answers:

2

I have some problem when i work with UIImagePickerController .when I presentModalViewController,and the picker pops up right,but i can not select a picture! when i touch on the picture list, the list can only scroll up and down,but can not select!

how about this? thanks.

A: 

You must implement the UIImagePickerControllerDelegate method

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
adam
A: 

same one have the same problem,it is a bug? look this: http://stackoverflow.com/questions/2558417/uiimagepickercontroller-does-nothing-when-using-camera-after-i-hit-use-button

here is the code,as you see,i do implement the delegate,but it does not work:

    uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    viewController = [[MainViewController alloc] init];

    [uiwindow addSubview:viewController.view];


@interface CameraViewController : UIViewController 
<UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UIImageView *imageView;
    UIButton *takePictureButton;
    UIButton *selectFromCameraRollButton;
    UIButton *pickFromLibrary;
}
@property (nonatomic, retain)  UIImageView *imageViewa;

- (void)getCameraPicture:(id)sender;
- (void)selectExistingPicture;
- (void)deLoad;

@end



@interface MainViewController : UIViewController{
    CameraViewController *cameraControl;
    Ems_view *ems_view;
}

@property (nonatomic,retain) CameraViewController *cameraControl;
@property (readonly,retain) Ems_view *ems_view;

- (void) ShowCamera;

@end


- (void) ShowCamera{
    if( cameraControl == NULL ) cameraControl = [[CameraViewController alloc] init];
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:cameraControl];
    [self presentModalViewController:nav animated:YES];
}


//
//  CameraViewController.m
//  Camera
//
//  Created by jeff on 4/29/09.
//  Copyright Jeff LaMarche 2009. All rights reserved.
//

#import "CameraViewController.h"
#import "Ems_appDelegate.h"
#import "MainViewController.h"
#import <arch/ems_camera_api.h>


#if IPHONE_CAMERA
int EMS_CameraCapture( dev_cameracapture_t *lpcamera )
{
    [[Ems_appDelegate sharedAppDelegate].viewController ShowCamera];
    return 1;
}
#endif


@implementation CameraViewController
@synthesize imageView;


- (void)loadView{

    UIView *aview=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = aview;
    [aview setBackgroundColor:[UIColor whiteColor]];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
    [aview addSubview:imageView];
    takePictureButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//] initWithFrame:CGRectMake(10, 450, 30, 20)];
    takePictureButton.frame = CGRectMake(10, 400, 80, 30);
    [takePictureButton setTitle:@"照相" forState:UIControlStateNormal];
    [takePictureButton addTarget:self action:@selector(getCameraPicture:) forControlEvents:UIControlEventTouchUpInside];
    [aview addSubview:takePictureButton];

    selectFromCameraRollButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];// initWithFrame:CGRectMake(50, 450, 30, 20)];
    selectFromCameraRollButton.frame = CGRectMake(100, 400, 100, 30);
    [selectFromCameraRollButton setTitle:@"从相机中选取" forState:UIControlStateNormal];
    [selectFromCameraRollButton addTarget:self action:@selector(getCameraPicture:) forControlEvents:UIControlEventTouchUpInside];
    [aview addSubview:selectFromCameraRollButton];
    //self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"从相机中选取" style:UIBarButtonItemStylePlain target:self action:@selector(getCameraPicture:)] autorelease];

    pickFromLibrary = [UIButton buttonWithType:UIButtonTypeRoundedRect];//] initWithFrame:CGRectMake(10, 450, 30, 20)];
    pickFromLibrary.frame = CGRectMake(210, 400, 100, 30);
    [pickFromLibrary setTitle:@"从相册中选取" forState:UIControlStateNormal];
    [pickFromLibrary addTarget:self action:@selector(selectExistingPicture) forControlEvents:UIControlEventTouchUpInside];
    [aview addSubview:pickFromLibrary];

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回" 
            style:UIBarButtonItemStylePlain target:self action:@selector(deLoad)] autorelease];


    [aview release];


}

- (void) deLoad{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    if (![UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeCamera]) {
        takePictureButton.hidden = YES;
        selectFromCameraRollButton.hidden = YES;
    }
}

- (void)viewDidUnload {
    imageView = nil;
    takePictureButton = nil;
    selectFromCameraRollButton = nil;
    pickFromLibrary = nil;
    //[super viewDidUnload];
}

- (void)dealloc {
    [imageView release];
    [takePictureButton release];
    [selectFromCameraRollButton release];
    [pickFromLibrary release];
    [super dealloc];
}

#pragma mark -
- (void)getCameraPicture:(id)sender {
    UIImagePickerController *picker =
    [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsImageEditing = YES;
    picker.sourceType = (sender == takePictureButton) ? 
    UIImagePickerControllerSourceTypeCamera :
    UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    //[self presentModalViewController:picker animated:YES];
    [self presentModalViewController:picker animated:YES];
    [picker release];

}
- (void)selectExistingPicture {
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker =
        [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsImageEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    } 
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"Error accessing photo library" 
                              message:@"Device does not support a photo library" 
                              delegate:nil 
                              cancelButtonTitle:@"Drat!" 
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
#pragma mark  -


- (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)image 
                  editingInfo:(NSDictionary *)editingInfo {
    imageView.image = [image retain];
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
}

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