• 相机拍照


    通过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
  • 相关阅读:
    模块化 —— CommonJS、AMD、UMD、ESM(下)
    模块化 —— CommonJS、AMD、UMD、ESM(上)
    移动端事件(四)—— 函数防抖和函数节流
    移动端事件(三)—— 横竖屏与加速度、移动的方块
    一起来学JavaScript吧(JS兔子领进门)
    【资源】一些常用的前端资源网站(不定期更新)
    redis
    django-高并发解决方案--负载均衡
    输入某年某月某日,判断这一天是这一年的第几天
    一篇英文文档中找出频数最多的10个单词
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9722183.html
Copyright © 2020-2023  润新知