使用相机和放大器相片集

时间:2014-02-16 03:30:20

标签: ios objective-c camera

我正在尝试打开一个用于打开相机的按钮和一个用于打开相机胶卷的按钮。这是我的代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)getCameraPicture:(id)sender{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.allowsEditing = NO;
    picker.sourceType = (sender == takePictureButton) ?
    UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}

- (IBAction)selectExistingPicture {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.allowsEditing = NO;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Device does not support photo library" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
    }
}

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



- (void)viewDidLoad
{
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        takePictureButton.hidden = YES;
    }
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

关于它为什么不起作用的任何想法?一切都是链接的,但按钮什么都不做

由于

修改

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIImagePickerControllerDelegate> {
    IBOutlet UIButton *takePictureButton;
    IBOutlet UIImageView *imageView;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIButton *takePictureButton;

- (IBAction)getCameraPicture:(id)sender;
- (IBAction)selectExistingPicture;

@end

1 个答案:

答案 0 :(得分:2)

只需添加:

[self presentViewController:picker animated:YES completion:nil];

在'getCameraPicture:'结尾处以及'selectExistingPicture:'。

您还可能希望将“UIImagePickerControllerDelegate”添加到视图控制器的界面中,以便调用“didFinishPickingImage”。

编辑:

你应该替换你的:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;

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

因为该方法已被弃用。

像这样使用:

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

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

来自http://www.appcoda.com/ios-programming-camera-iphone-app/的代码。