拍照使用Xcode中的故事板或Xib

时间:2014-01-29 10:27:35

标签: ios iphone

我想用Stroyboard拍照(因为我没有iPhone)

知道我有代码拍照和选择

然后

使用iPhone中调整/优化图像大小的最简单方法是

·H

#import <UIKit/UIKit.h>

@interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)takePhoto:  (UIButton *)sender;
- (IBAction)selectPhoto:(UIButton *)sender;

@end

.M

#import "APPViewController.h"

@interface APPViewController ()

@end

@implementation APPViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Device has no camera"
                                                        delegate:nil
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles: nil];

        [myAlertView show];

    }

}

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

}

- (IBAction)takePhoto:(UIButton *)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

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

}

- (IBAction)selectPhoto:(UIButton *)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

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


}

#pragma mark - Image Picker Controller delegate methods

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

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

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

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

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

@end

1 个答案:

答案 0 :(得分:0)

您必须使用 UIImagePickerController

检查相机是否可用
  

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;

对于您的场景,您应该编写 takePhoto 方法:

- (IBAction)takePhoto:(UIButton *)sender {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            //Do your stuff here
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message: @"Camera is not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];      
        }
    }

修改

使用模拟器无法拍照。您必须要求设备拍照或打开相机。

它可以帮到你。

快乐的编码......:)