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
  • 相关阅读:
    第十四回 基础才是重中之重~委托实例的几种定义方式(规规矩矩method,逻辑简单delegate,层次清晰lambda)
    第十五回 基础才是重中之重~老赵写的CodeTimer是代码性能测试的利器
    第十一回 基础才是重中之重~Conditional特性使代码根据条件在debug或者release模式中执行
    第十二回 基础才是重中之重~.net中的显式事务与隐式事务
    IE绝对定位元素神秘消失或被遮挡的解决方法
    图片按钮样式隐藏文字的
    javascript 保留小数
    Javascript兼容性问题小结(容易导致浏览器不同,又不容易察觉的)
    SQL 随机选取10条数据
    动态添加类和元素样式属性(style and className)
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9722183.html
Copyright © 2011-2022 走看看