IOS SDK详解之拍照/相册(默认+自定义拍照界面)

原创blog,转载请注明出处

blog.csdn.net/hello_hwc



前言:

本来要更新NSURLSession的UploadTask的,结果写那个Demo的时候想要写成拍照上传,然后就想到先写一个关于拍照的Demo吧。本文会先介绍下如何使用系统提供的界面拍照和选择相册,然后自定义拍照界面。注意,本文使用的是UIImagePickerController,所以不能完全的自定义,如果想要彻底的自定义拍照,建议选择AV Foundation这个框架来做



Demo效果

进入系统的拍照界面



进入自定义拍照界面



自定义前置摄像头和后置摄像头切换动画-翻页


一 使用系统提供的界面拍照和相册选择

第一步

保存一个UIImagePickerController的实例,然后适当的时候初始化始化。Demo选择在viewDidLoad初始化。让当前类实现UIImagePickerControllerDelegate,UINavigationControllerDelegate两个代理

@property (strong,nonatomic)UIImagePickerController * imagePikerViewController;
//初始化
self.imagePikerViewController = [[UIImagePickerController alloc] init];
self.imagePikerViewController.delegate = self;//通过代理来传递拍照的图片
self.imagePikerViewController.allowsEditing = YES;//允许编辑

第二步,通过ActionSheet来让用户选择是拍照还是到相册选择,然后模态的显示

[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];

注意,要先判断相机是否可用,然后在进入相机(有可能相机坏了,或者在虚拟机上运行的)

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
                                                                              message: nil
                                                                       preferredStyle:UIAlertControllerStyleActionSheet];

    [alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.imagePikerViewController animated:YES completion:NULL];
        }else{
            [self showAlertWithMessage:@"Camera is not available in this device or simulator"];
        }
    }]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
            self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagePikerViewController animated:YES completion:NULL];
        }
    }]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"Cancel" style: UIAlertActionStyleCancel handler:nil]];
    [self presentViewController: alertController animated: YES completion: nil];

第三部,代理函数处理拍照或者cancel事件

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage * image = info[UIImagePickerControllerEditedImage];
    if (!image) {
        image = info[UIImagePickerControllerOriginalImage];
    }
    self.imageview.image = image;
    [self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

二 自定义拍照界面

UIImagePickerController的自定义界面比较简单,通过设置cameraOverlayView这个属性为自定义的View就能自定义。

第一步 创建一个View

创建xib文件

拖拽控件,进行autoLayout,最终效果如图

把fileOwner设置成CustomTakePhotoViewController

第二步 在显示拍照界面之前,把UI设置成自己想要的,注意,把属性

showsCameraControls设置为NO,不让默认的界面出现。


self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePikerViewController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"CustomOverLayview" owner:self options:nil];
self.takePictureButton.layer.cornerRadius = 20;
self.takePictureButton.clipsToBounds = YES;
self.overlayView.frame = self.imagePikerViewController.cameraOverlayView.frame;
self.overlayView.backgroundColor = [UIColor clearColor];
self.imagePikerViewController.cameraOverlayView = self.overlayView;
self.overlayView = nil;
[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];

第三步,为view上的控件添加动作

Segment Control负责切换前置摄像头和后置摄像头,为了流畅,在切换的时候显示动画。

- (IBAction)cameraSelect:(UISegmentedControl *)sender{
    NSInteger index = sender.selectedSegmentIndex;

    if (index == 0) {
        [UIView transitionWithView:self.imagePikerViewController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCurlDown animations:^{
            self.imagePikerViewController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        } completion:NULL];
    }else{
        [UIView transitionWithView:self.imagePikerViewController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCurlUp animations:^{
            [self.imagePikerViewController setCameraDevice:UIImagePickerControllerCameraDeviceRear];
        } completion:NULL];
    }
}

拍照的Button

- (IBAction)takePicture:(id)sender {
    [self.imagePikerViewController takePicture];
}

取消的Button

- (IBAction)cancelTakePicture:(id)sender {
    [self dismissViewControllerAnimated:YES completion:NULL];
}

第四步 在代理中处理图片

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage * image = info[UIImagePickerControllerEditedImage];
    if (!image) {
        image = info[UIImagePickerControllerOriginalImage];
    }
    self.imageview.image = image;
    [self dismissViewControllerAnimated:YES completion:NULL];
}


注意,如果log输出

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

直接忽略就是了,没有任何影响,貌似是IOS 8的一个bug



下载链接

http://download.csdn.net/detail/hello_hwc/8553539

时间: 2024-08-29 23:20:53

IOS SDK详解之拍照/相册(默认+自定义拍照界面)的相关文章

iOS SDK详解之NSScanner-分析String

原创blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK详解专栏,这里有很多基础的文章 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言:NSScanner是分析String,把String转为substring和数字的很好的工具.它使用一个NSString初始化,使用的时候通常从开头处扫描直到结尾. 本文会先举出两个例子,然后详细的讲解NSScanner的方法.源码是

iOS SDK详解之NSCoding协议

原创blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=contents 欢迎关注我的iOS SDK详解专栏 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言:NSCoding是对iOS中的Model类进行编码和解码必须要遵循的协议,如果一个对象要被归档,那么这个协议是必须的. NSCoding要实现两个方法 - initWithCoder: //解码 - enc

IOS SDK详解

来源:http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html?page=1#42803301 博客专栏>移动开发专栏>IOS SDK详解 分享到:新浪微博腾讯微博IOS SDK详解 本专栏从IOS SDK中常用的Framework出发,继而深入的介绍各个Framework.每个Framework博主都会进行Demo 收藏 订阅 最新更新文章 [移动开发] IOS SDK详解之CALayer(二) 原创Blog,转载请注明出处

iOS SDK详解之IBInspectable和IB_DESIGNABLE-Storyboad动态刷新

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS-SDK详解专栏,在这里你能找到很多iOS开发基础的文章 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言: 在做应用的UI设计的时候,如果属性能够在Interface Builder的图形化界面进行设置,并且动态的预览到效果,那样无疑会大大提高应用的开发效率.而XCode为我们提供了这样的一种方式,就是使用IBInspe

iOS SDK详解之NSCalendar & NSDate?Components

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢饮关注我的iOS SDK详解专栏 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言:NSCalendar 和 NSDate?Components是有关iOS 时间相关API很重要的两个类.最近刚好用到,这里就整理下. 概念 NSCalendar 顾名思义就是日历,封装了系统如何按照年月日的方式来组织时间,组织时间的方式和地区,时区有很大关

IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言:有两个月左右没为公司开发IOS项目了(最近一直在搞IOT),以至于对IOS 8的这个更新都没看到.这里补上. 一 概述 在IOS8之后,UIAlertController替代了UIActionSheet和UIAlertView.把两种类型的提示信息放到这一个类里来实现. 注意, 这个class不能通过继承的方式来自定义. 二 类介绍 先举两个使用的例子 例子一 UIAlertController * alertC

IOS SDK详解之KVC

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言:本文的架构 KVC的定义 KVC的几个场景 希望,通过本文让不了解KVC的同学入门,KVC在IOS开发中是个比较重要的概念,也是理解KVO的基础. 一 KVC的定义 KVC的全称是key-value coding,通过key-value的方式来访问属性.在很多地方,KVC是非常方便的. 属性 @property (strong,nonatomic) NSString * message; 赋值 [self set

iOS SDK详解之模糊(毛玻璃)效果效果

原创blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=list 前言: 在iOS 8 之前,想要实现模糊效果,一般会使用一些Github库,当然自己定制也可以,其原理就是用Core Image进行一些数字图像处理(因为电子出身,本课的时候做过,用矩阵来做).不过,到了iOS 8之后,这一切变的非常简单,因为Apple公开了之前的几个私有API. Demo效果 三种Blur Vibrancy(也就是在Blur上加一些想要强调的部分) Demo

IOS SDK详解之沙盒(一)图解+小工具

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言: IOS沙盒机制限制了App的访问权限,进而保护用户的数据信息. 一 查看沙盒结构 和一些百度来的博客显示隐藏稳文件的方式不同,本文也提供两种方式,简单粗暴. 方式一 使用工具simpholders(推荐) 下载链接 http://simpholders.com/ 效果如图 方式二 直接使用代码 用以下代码,log出documents/路径 NSURL * url = [[[NSFileManager defau