iOS 加载照相机

iOS 加载照相机

加载系统相机,需要声明UIImagePickerControllerDelegate
创建UIButton

 UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(150, 200, 100, 50)];
 [button setTitle:@"相机" forState:(UIControlStateNormal)];
 [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
 button.backgroundColor = [UIColor grayColor];
 [button addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];
 [self.view addSubview:button];
 self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake:(0,300,self.view.bounds.size.width,300)];
 [self.view addSubview:self.imageView];

点击button后执行的方法:

 -(void)click{
     UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
          sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     }
     UIImagePickerController * pick = [[UIImagePickerController alloc]init];
     pick.delegate = self;
     pick.allowsEditing = YES;
     pick.sourceType = sourceType;
     [self presentViewController:pick animated:YES completion:^{

     }];
}

UIImagePickerControllerDelegate的代理方法

//点击保存时调用的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:^{
     }];
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self performSelector:@selector(saveImage:) withObject:image afterDelay:0.5];
}
//点击取消是调用的方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}

赋值

-(void)saveImage:(UIImage *)image{
   _imageView.image = image;
}

简单相机调用

你可能感兴趣的:(iOS 加载照相机)