• 相机拍照


    通过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
  • 相关阅读:
    Grunt学习笔记【7】---- grunt-contrib-less插件详解
    Grunt学习笔记【6】---- grunt-contrib-requirejs插件详解
    Grunt学习笔记【5】---- expand使用方法
    Grunt学习笔记【4】---- 通配符和模板
    Grunt学习笔记【3】---- filter使用方式和实现原理详解
    Grunt 学习笔记【2】---- 配置和创建任务
    用css去除chrome、safari等webikt内核浏览器对控件默认样式
    angular中通过$location获取路径(参数)的写法
    angular自身带有的操作cookie的方法
    js中apply方法的使用
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9722183.html
Copyright © 2020-2023  润新知