zoukankan      html  css  js  c++  java
  • 相机拍照

    通过UIImagePickerController来实现调用手机相机或则打开手机相册

    (一)头文件

    #import <UIKit/UIKit.h>
    
    @interface CameraViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
    
    @end

    (二)初始化UIImagePickerController变量,并设置委托

    @interface CameraViewController ()
    {
        UIImagePickerController *imagePicker;
        UIImageView *imageView;
    }
    @end
    
    @implementation CameraViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor=[UIColor blackColor];
        
        imagePicker=[[UIImagePickerController alloc] init];
        imagePicker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        imagePicker.delegate=self;
        // Do any additional setup after loading the view.
    }

    @end

    (三)拍照

        imagePicker.allowsEditing = YES;
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera])
        {
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        

    (四)相册选择照片

        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    (五)实现委托

    不管是新拍的照片还是在相册中选择照片,最终会在操作结束后通过委托形式传递到ui上

    -(void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        [self dismissViewControllerAnimated:YES completion:^{}];
        
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        if (image == nil) {
            image = [info objectForKey:UIImagePickerControllerOriginalImage];
            NSLog(@"Image null");
        }
        imageView.image = image;
        NSLog(@"Photo selected");
    }
    
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [self dismissViewControllerAnimated:YES completion:^{}];
        NSLog(@"Photo dimiss");
    }

    //------------------------------------完整代码---------------------------------------------//

    #import "CameraViewController.h"
    
    @interface CameraViewController ()
    {
        UIImagePickerController *imagePicker;
        UIImageView *imageView;
    }
    @end
    
    @implementation CameraViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor=[UIColor blackColor];
        self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
        
        imageView=[[UIImageView alloc] init];
        imageView.frame=CGRectMake(100, 50, 200, 300);
        UIImage *uiImage=[UIImage imageNamed:@"Example.png"];
        [imageView setImage:uiImage];
        [self.view addSubview:imageView];
        
        UIButton *pickBtn=[[UIButton alloc] initWithFrame:CGRectMake(90, 400, 100, 30)];
        pickBtn.backgroundColor=[UIColor redColor];
        [pickBtn setTitle:@"pick" forState:UIControlStateNormal];
        [pickBtn addTarget:self action:@selector(pickImage) forControlEvents:UIControlEventTouchDown];
        
        UIButton *takeBtn=[[UIButton alloc] initWithFrame:CGRectMake(210, 400, 100, 30)];
        takeBtn.backgroundColor=[UIColor redColor];
        [takeBtn setTitle:@"take" forState:UIControlStateNormal];
        [takeBtn addTarget:self action:@selector(takeImage) forControlEvents:UIControlEventTouchDown];
        
        [self.view addSubview:pickBtn];
        [self.view addSubview:takeBtn];
        
        imagePicker=[[UIImagePickerController alloc] init];
        imagePicker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        imagePicker.delegate=self;
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void) pickImage
    {
        NSLog(@"pick");
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [self presentViewController:imagePicker animated:YES completion:^{}];
    }
    
    -(void)takeImage
    {
        NSLog(@"take");
        imagePicker.allowsEditing = YES;
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera])
        {
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        
        [self presentViewController:imagePicker animated:YES completion:^{}];
        
    }
    
    -(void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        [self dismissViewControllerAnimated:YES completion:^{}];
        
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        if (image == nil) {
            image = [info objectForKey:UIImagePickerControllerOriginalImage];
            NSLog(@"Image null");
        }
        imageView.image = image;
        NSLog(@"Photo selected");
    }
    
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [self dismissViewControllerAnimated:YES completion:^{}];
        NSLog(@"Photo dimiss");
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
  • 相关阅读:
    java技术用ssh从linux服务器下载数据
    linux 常见操作命令
    IP地址归属地查询
    【转】Java检测字符串是否有乱码
    maven install 跳过test方法
    echarts实现动态传入数据刷新【可执行】
    echarts报错Cannot read property 'features' of undefined
    【java web】Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    程序员,我们都是夜归人【转】
    程序员你为什么这么忙?【转】
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9722183.html
Copyright © 2011-2022 走看看