iOS开发中訪问相冊摄像像头

源代码下载地址http://download.csdn.net/download/jingjingxujiayou/7270479

在AppDelegate.m文件里

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. // Override point for customization after application launch.
  5. self.window.backgroundColor = [UIColor whiteColor];
  6. [self.window makeKeyAndVisible];
  7. self.window.rootViewController = [[dateViewController alloc]init];
  8. return YES;
  9. }

dateViewController.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface dateViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationBarDelegate>
  4. @property(nonatomic,retain)UIImageView* imageview1;
  5. @property(nonatomic,retain)UIImagePickerController* imagepicker;
  6. @end

dateViewController.m

  1. //
  2. // dateViewController.m
  3. // datepick
  4. //
  5. // Created by 5_2 on 14-4-29.
  6. // Copyright (c) 2014年 Frountion. All rights reserved.
  7. //
  8.  
  9. #import "dateViewController.h"
  10.  
  11. @interface dateViewController ()
  12.  
  13. @end
  14.  
  15. @implementation dateViewController
  16. @synthesize imageview1,imagepicker;
  17.  
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  19. {
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self) {
  22. // Custom initialization
  23. }
  24. return self;
  25. }
  26.  
  27. - (void)viewDidLoad
  28. {
  29. [super viewDidLoad];
  30. // Do any additional setup after loading the view.
  31.  
  32. //----------1 从网络载入图片
  33. imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 50, 300, 200)];
  34. imageview1.backgroundColor = [UIColor yellowColor];
  35. [self.view addSubview:imageview1];
  36. NSURL* url = [[NSURL alloc]initWithString:@"https://www.google.com.hk/images/srpr/logo11w.png"];
  37. NSData* data = [NSData dataWithContentsOfURL:url];
  38. //把data转成image
  39. UIImage* image = [UIImage imageWithData:data];
  40. //显示图片
  41. imageview1.image = image;
  42.  
  43. //把图片转化为数据
  44. NSData* imagedata = UIImageJPEGRepresentation(image, 1);
  45. NSLog(@"%d",imagedata.length);
  46.  
  47. //保存到相冊里面,这个能够到模拟器里的相冊产查看的。
  48. UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
  49.  
  50. //---------2 相冊的訪问
  51.  
  52. UIButton *buttonphoto = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  53. buttonphoto.tag = 100;
  54. buttonphoto.frame = CGRectMake(50, 250, 80, 50);
  55. [buttonphoto setTitle:@"訪问相冊" forState:UIControlStateNormal];
  56. [buttonphoto addTarget:self action:@selector(look:) forControlEvents:UIControlEventTouchUpInside];
  57. [self.view addSubview:buttonphoto];
  58.  
  59. //---------3 摄像头的訪问
  60. UIButton *buttoncamera = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  61. buttoncamera.tag = 200;
  62. buttoncamera.frame = CGRectMake(50, 310, 80, 50);
  63. [buttoncamera setTitle:@"訪问摄像头" forState:UIControlStateNormal];
  64. [buttoncamera addTarget:self action:@selector(look:) forControlEvents:UIControlEventTouchUpInside];
  65. [self.view addSubview:buttoncamera];
  66.  
  67. }
  68.  
  69. /*-(void)look:(UIButton*)button
  70. {
  71. UIImagePickerController* imagepicker = [[UIImagePickerController alloc]init];
  72. imagepicker.delegate = self;
  73. //訪问相冊类型的类型
  74. //UIImagePickerControllerSourceTypePhotoLibrary,
  75. //UIImagePickerControllerSourceTypeCamera, ===== 訪问摄像头
  76. //UIImagePickerControllerSourceTypeSavedPhotosAlbum ======= 仅仅能訪问第一列的图片
  77. imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  78. //以摩擦动画的方式显示
  79. [self presentViewController:imagepicker animated:YES completion:^{
  80.  
  81. }];
  82.  
  83. if (button.tag == 200) {
  84. //UIImagePickerControllerCameraDeviceFront === 前摄像头
  85. //UIImagePickerControllerCameraDeviceRear === 后摄像头
  86. BOOL isCamrma = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
  87. if (!isCamrma) {
  88. NSLog(@"没有摄像头");
  89. return;
  90. }
  91. //摄像头
  92. imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  93. //同意编辑
  94. imagepicker.allowsEditing =YES;
  95. }
  96. }*/
  97.  
  98. -(void)look:(UIButton*)button
  99. {
  100. if (button.tag == 100) {
  101. imagepicker = [[UIImagePickerController alloc]init];
  102. imagepicker.delegate = self;
  103. //訪问相冊类型的类型
  104. //UIImagePickerControllerSourceTypePhotoLibrary,
  105. //UIImagePickerControllerSourceTypeCamera, ===== 訪问摄像头
  106. //UIImagePickerControllerSourceTypeSavedPhotosAlbum ======= 仅仅能訪问第一列的图片
  107. imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  108. //以摩擦动画的方式显示
  109. [self presentViewController:imagepicker animated:YES completion:^{
  110.  
  111. }];
  112.  
  113. }else {
  114.  
  115. //注意摄像头的訪问须要在真机上进行
  116.  
  117. //UIImagePickerControllerCameraDeviceFront === 前摄像头
  118. //UIImagePickerControllerCameraDeviceRear === 后摄像头
  119. BOOL isCamrma = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
  120. if (!isCamrma) {
  121. NSLog(@"没有摄像头");
  122. return;
  123. }
  124. //摄像头
  125. imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  126. //同意编辑
  127. imagepicker.allowsEditing =YES;
  128. }
  129. }
  130.  
  131. //---------2 相冊的訪问
  132. #pragma mark - UIImagePickerControllerDelegate
  133.  
  134. //相冊选中之后调用
  135. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  136. {
  137. //UIImagePickerControllerOriginalImage === 取原始图片
  138. //UIImagePickerControllerEditedImage === 去编辑以后的图片
  139. UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
  140. imageview1.image = image;
  141. NSLog(@"info = %@",info);
  142. [picker dismissViewControllerAnimated:YES completion:nil];
  143. }
  144. //取消按钮的点击事件
  145. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  146. {
  147. [picker dismissViewControllerAnimated:YES completion:NULL];
  148. }
  149. //将图片保存
  150. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  151. {
  152. NSLog(@"error = %@",error);
  153. }
  154.  
  155. - (void)didReceiveMemoryWarning
  156. {
  157. [super didReceiveMemoryWarning];
  158. // Dispose of any resources that can be recreated.
  159. }
  160.  
  161. @end

iOS开发中訪问相冊摄像像头的更多相关文章

  1. iOS开发中的MVC设计模式

    我们今天谈谈cocoa程序设计中的 模型-视图-控制器(MVC)范型.我们将从两大方面来讨论MVC: 什么是MVC? M.V.C之间的交流方式是什么样子的? 理解了MVC的概念,对cocoa程序开发是 ...

  2. IOS开发中UI编写方式——code vs. xib vs.StoryBoard

    最近接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问,就是应该如何制作UI界面.iOS应用是非常重视用户体验的,可以说绝大多数的应用成功与否与交互设计以及UI是否漂亮易用有着非常大的关 ...

  3. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

  4. iOS开发中你是否遇到这些经验问题

    前言 小伙伴们在开发中难免会遇到问题, 你是如何解决问题的?不妨也分享给大家!如果此文章其中的任何一条问题对大家有帮助,那么它的存在是有意义的! 反正不管怎样遇到问题就要去解决问题, 在解决问题的同时 ...

  5. iOS 开发中常见的设计模式

    最近有小伙伴问到在iOS开发中的几种设计模式,这里摘录一下别人的总结(因为已经感觉总结得差不多了,适用的可以阅读一下) 首先是开发中的23中设计模式分为三大类:1.创建型 2.结构型 3.行为型 (i ...

  6. iOS开发中遇到的一些问题及解决方案【转载】

    iOS开发中遇到的一些问题及解决方案[转载] 2015-12-29 [385][scrollView不接受点击事件,是因为事件传递失败] // //  MyScrollView.m //  Creat ...

  7. iOS开发中各种关键字的区别

    1.一些概念 1.浅Copy:指针的复制,只是多了一个指向这块内存的指针,共用一块内存. 深Copy:内存的复制,两块内存是完全不同的, 也就是两个对象指针分别指向不同的内存,互不干涉. 2.atom ...

  8. 总结iOS开发中的断点续传那些事儿

    前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...

  9. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

随机推荐

  1. [bzoj4816][Sdoi2017]数字表格 (反演+逆元)

    (真不想做莫比乌斯了) 首先根据题意写出式子 ∏(i=1~n)∏(j=1~m)f[gcd(i,j)] 很明显的f可以预处理出来,解决 根据套路分析,我们可以先枚举gcd(i,j)==d ∏(d=1~n ...

  2. Java集合(三)--Collection、Collections和Arrays

    Collection: Collection是集合类的顶级接口,提供了对集合对象进行基本操作的通用接口方法.Collection接口的意义是为各种具体的集合提供了最大化 的统一操作方式,其直接继承接口 ...

  3. numpy add

    在numpy中,'+' 和add 是一样的 np.add(x1, x2) x1+x2 有种特殊情况需要注意,x1和x2的shape不一样的加法: 两个shape不一样的array相加后会变成一个com ...

  4. 安装Yellowfin报错——No such file or directory: '/tmp/pip-build-jykvuD/YellowFin/README.md'

    https://blog.csdn.net/quqiaoluo5620/article/details/80608474 在Pycharm中安装Yellowfin时一直报错"no such ...

  5. pycharm激活2018

    因为我的是Windows,所以这篇文章只针对Windows系统. 1.将“0.0.0.0 account.jetbrains.com”中的内容添加到hosts文件中,hosts路径为:C:\Windo ...

  6. “完美”解决微信小程序购物车抛物动画,在连续点击时出现计算错误问题,定时器停不下来。

    最近做,微信点餐小程序,遇到添加商品时出现抛物动画,参考借鉴了这位大神的方法 https://www.cnblogs.com/greengage/p/7815842.html 但出现了一个问题,连续点 ...

  7. Yahoo前端优化的35条军规

    摘要:无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化34条军规,不过现在已经是35条了,所以可以说是雅虎前端优化的35条军 ...

  8. 编译时报错,找不到指定路径下的command,而路径是正确的。

    使用的Fedora 18 64位的系统kernel,内核为3.6.10.按照要求使用yum install *** 安装各项工具. path路径使用提供的toolchain,各种路径也安装正确,却发现 ...

  9. add list of symbols in latex

    * add list of symbols -- latex 1. createa new tex file named "toc.tex' with the following conte ...

  10. LeetCode(59)SPiral Matrix II

    题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...