IOS开发---菜鸟学习之路--(十五)-如何实现拍照功能

简介: 本章将来讲解下如何实现拍照的功能 我们需要的实现的效果是       好了 直接开始内容吧 首先我们需要新建一个ViewController 就叫AddPictureViewController 然后选择.

本章将来讲解下如何实现拍照的功能

我们需要的实现的效果是 

    

好了 直接开始内容吧

首先我们需要新建一个ViewController

就叫AddPictureViewController

然后选择.h文件进行如下修改

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface AddPictureViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
 4 {
 5     UITextView *contenttextview;
 6     UIImageView *contentimageview;
 7     NSString *lastChosenMediaType;
 8 
 9 }
10 @property(nonatomic,retain) IBOutlet UITextView *contenttextview;
11 @property(nonatomic,retain)  IBOutlet   UIImageView *contentimageview;
12 @property(nonatomic,copy)        NSString *lastChosenMediaType;
13 -(IBAction)buttonclick:(id)sender;
14 
15 @end
AddPictureViewController.h

我们需要添加以下两个库

QuartzCore

MobileCoreServices

在项目的General里找到  linked Frameworks and libraries 添加两个类库

然后修改XIB文件

并建立相关链接

最后就是.m文件部分的代码了。

我就直接上代码了 大家可以直接复制过去 然后再理解

  1 #import "AddPictureViewController.h"
  2 #import <QuartzCore/QuartzCore.h>
  3 #import <QuartzCore/CoreAnimation.h>
  4 #import <MobileCoreServices/UTCoreTypes.h>
  5 @interface AddPictureViewController ()
  6 
  7 @end
  8 
  9 @implementation AddPictureViewController
 10 @synthesize contentimageview;
 11 @synthesize contenttextview;
 12 @synthesize lastChosenMediaType;
 13 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 14 {
 15     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 16     if (self) {
 17         // Custom initialization
 18     }
 19     return self;
 20 }
 21 
 22 - (void)viewDidLoad
 23 {
 24     [super viewDidLoad];
 25     // Do any additional setup after loading the view from its nib.
 26 }
 27 -(IBAction)buttonclick:(id)sender
 28 {
 29     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"请选择图片来源" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从手机相册选择", nil];
 30     [alert show];
 31 }
 32 - (void)didReceiveMemoryWarning
 33 {
 34     [super didReceiveMemoryWarning];
 35     // Dispose of any resources that can be recreated.
 36 }
 37 #pragma 拍照选择模块
 38 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
 39     if(buttonIndex==1)
 40         [self shootPiicturePrVideo];
 41     else if(buttonIndex==2)
 42         [self selectExistingPictureOrVideo];
 43 }
 44 #pragma  mark- 拍照模块
 45 //从相机上选择
 46 -(void)shootPiicturePrVideo{
 47     [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
 48 }
 49 //从相册中选择
 50 -(void)selectExistingPictureOrVideo{
 51     [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
 52 }
 53 #pragma 拍照模块
 54 -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
 55     self.lastChosenMediaType=[info objectForKey:UIImagePickerControllerMediaType];
 56     if([lastChosenMediaType isEqual:(NSString *) kUTTypeImage])
 57     {
 58         UIImage *chosenImage=[info objectForKey:UIImagePickerControllerEditedImage];
 59         contentimageview.image=chosenImage;
 60     }
 61     if([lastChosenMediaType isEqual:(NSString *) kUTTypeMovie])
 62     {
 63         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示信息!" message:@"系统只支持图片格式" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
 64         [alert show];
 65         
 66     }
 67     [picker dismissModalViewControllerAnimated:YES];
 68 }
 69 -(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
 70     [picker dismissModalViewControllerAnimated:YES];
 71 }
 72 -(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
 73 {
 74     NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
 75     if([UIImagePickerController isSourceTypeAvailable:sourceType] &&[mediatypes count]>0){
 76         NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
 77         UIImagePickerController *picker=[[UIImagePickerController alloc] init];
 78         picker.mediaTypes=mediatypes;
 79         picker.delegate=self;
 80         picker.allowsEditing=YES;
 81         picker.sourceType=sourceType;
 82         NSString *requiredmediatype=(NSString *)kUTTypeImage;
 83         NSArray *arrmediatypes=[NSArray arrayWithObject:requiredmediatype];
 84         [picker setMediaTypes:arrmediatypes];
 85         [self presentModalViewController:picker animated:YES];
 86     }
 87     else{
 88         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
 89         [alert show];
 90     }
 91 }
 92 static UIImage *shrinkImage(UIImage *orignal,CGSize size)
 93 {
 94     CGFloat scale=[UIScreen mainScreen].scale;
 95     CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
 96     CGContextRef context=CGBitmapContextCreate(NULL, size.width *scale,size.height*scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
 97     CGContextDrawImage(context, CGRectMake(0, 0, size.width*scale, size.height*scale), orignal.CGImage);
 98     CGImageRef shrunken=CGBitmapContextCreateImage(context);
 99     UIImage *final=[UIImage imageWithCGImage:shrunken];
100     CGContextRelease(context);
101     CGImageRelease(shrunken);
102     return  final;
103 }
104 
105 @end
AddPictureViewController.m

最后就实现效果了

目录
相关文章
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
913 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
471 66
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
571 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
监控 搜索推荐 数据安全/隐私保护
深入探索iOS 14的隐私保护功能
本文将深入探讨iOS 14操作系统中的隐私保护功能,包括新的隐私指示器、应用程序跟踪透明度以及增强的隐私设置。我们将分析这些功能如何提高用户对个人数据的控制权,并讨论它们对应用开发者和广告行业的影响。
534 28
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
1789 11
|
人工智能 程序员 API
iOS|记一名 iOS 开发新手的前两次 App 审核经历
啥,这玩意也有新手保护期?
450 0
|
开发框架 Android开发 iOS开发
安卓与iOS开发中的跨平台策略:一次编码,多平台部署
在移动应用开发的广阔天地中,安卓和iOS两大阵营各占一方。随着技术的发展,跨平台开发框架应运而生,它们承诺着“一次编码,到处运行”的便捷。本文将深入探讨跨平台开发的现状、挑战以及未来趋势,同时通过代码示例揭示跨平台工具的实际运用。
437 3
|
Java 调度 Android开发
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。
|
存储 安全 数据安全/隐私保护
深入解析iOS 14隐私保护功能:用户数据安全的新里程碑
随着数字时代的到来,个人隐私保护成为全球关注的焦点。苹果公司在最新的iOS 14系统中引入了一系列创新的隐私保护功能,旨在为用户提供更透明的数据使用信息和更强的控制权。本文将深入探讨iOS 14中的几项关键隐私功能,包括App跟踪透明性、简化的隐私设置以及增强的系统安全性,分析它们如何共同作用以提升用户的隐私保护水平。
906 3
|
存储 前端开发 Swift
探索iOS开发:从新手到专家的旅程
本文将带您领略iOS开发的奇妙之旅,从基础概念的理解到高级技巧的掌握,逐步深入iOS的世界。文章不仅分享技术知识,还鼓励读者在编程之路上保持好奇心和创新精神,实现个人成长与技术突破。