原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址:IOS研究院之打开照相机与本地相册选择图片(六)

Hello 大家好 IOS的文章好久都木有更新了,今天更新一篇哈。 这篇文章主要学习如何在IOS程序中打开照相机与本地相册并且选择一张图片。还是老样子MOMO写了一个简单的测试程序,如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样。最终点击发送将按钮将图片2进制图片上传服务器。

下面我们仔细学习具体的细节。创建一个空的IOS项目,接着在创建一个ViewController。

AppDelegate.h 应用的代理类 这个没什么好说的就是直接打开刚刚创建的新ViewController。

#import <UIKit/UIKit.h>
#import "TestViewController.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navController;
@property (strong, nonatomic) UIViewController *viewController;
@end

AppDelegate.m 在这里就是打开我们创建的TestViewController

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize navController;
@synthesize viewController; - (void)dealloc
{
[_window release];
[super dealloc];
} - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor whiteColor];
self.viewController = [[TestViewController alloc]init];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navController.view]; [self.window makeKeyAndVisible];
return YES;
} @end

TestViewController.h 注意这里面引入了很多代理类。

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
//输入框
UITextView *_textEditor; //下拉菜单
UIActionSheet *myActionSheet; //图片2进制路径
NSString* filePath;
}
@end

TestViewController.m 请大家仔细看这个类, 所有的东西都写在了这里哈。

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (void)viewDidLoad
{
[super viewDidLoad];
//导航栏标题
self.navigationItem.title = @"雨松MOMO输入框"; //导航栏按钮
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle: @"发送"
style: UIBarButtonItemStyleDone
target: self
action: @selector(sendInfo)] autorelease]; //输入框显示区域
_textEditor = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
//设置它的代理
_textEditor.delegate = self;
_textEditor.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_textEditor.keyboardType = UIKeyboardTypeDefault;
_textEditor.font = [UIFont systemFontOfSize:20];
_textEditor.text = @"请输入内容"; //默认软键盘是在触摸区域后才会打开
//这里表示进入当前ViewController直接打开软键盘
[_textEditor becomeFirstResponder]; //把输入框加在视图中
[self.view addSubview:_textEditor]; //下方的图片按钮 点击后呼出菜单 打开摄像机 查找本地相册
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"camera" ofType:@"png"]]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 120, image.size.width, image.size.height); [button setImage:image forState:UIControlStateNormal]; [button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside]; //把它也加在视图当中
[self.view addSubview:button]; } -(void)openMenu
{
//在这里呼出下方菜单按钮项
myActionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles: @"打开照相机", @"从手机相册获取",nil]; [myActionSheet showInView:self.view];
[myActionSheet release]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{ //呼出的菜单按钮点击后的响应
if (buttonIndex == myActionSheet.cancelButtonIndex)
{
NSLog(@"取消");
} switch (buttonIndex)
{
case 0: //打开照相机拍照
[self takePhoto];
break; case 1: //打开本地相册
[self LocalPhoto];
break;
}
} //开始拍照
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[picker release];
[self presentModalViewController:picker animated:YES];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
} //打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentModalViewController:picker animated:YES];
[picker release];
} //当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; //当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 1.0);
}
else
{
data = UIImagePNGRepresentation(image);
} //图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager]; //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil]; //得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"]; //关闭相册界面
[picker dismissModalViewControllerAnimated:YES]; //创建一个选择后图片的小图标放在下方
//类似微薄选择图后的效果
UIImageView *smallimage = [[[UIImageView alloc] initWithFrame:
CGRectMake(50, 120, 40, 40)] autorelease]; smallimage.image = image;
//加在视图中
[self.view addSubview:smallimage]; } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"您取消了选择图片");
[picker dismissModalViewControllerAnimated:YES];
} -(void)sendInfo
{
NSLog(@"图片的路径是:%@", filePath); NSLog(@"您输入框中的内容是:%@", _textEditor.text);
} - (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
} @end

如下图所示,打开下拉菜单按钮开始选择打开相机 或者 打开本地相册。模拟器中是无法打开照相机的的,切记。

如下图所示,这里就是我本地的相册啦,里面保存了几张图片,选择一张即可。

我在这里再说说图片上传, 图片上传我们采用的是2进制ASIHTTPRequest 来完成的。

发送请求

    NSString *server_base = [NSString stringWithFormat:@"%@/users/uploadResource.json", _server];

    ASINetworkQueue *queue = [[ASINetworkQueue alloc] init];  

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:server_base]];

    [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: NO];
[request setDelegate :self];
[request setDidFinishSelector:@selector(sendCommentSucc:)];
[request setDidFailSelector:@selector(sendCommentFail:)];
// res 就是 需要上传图片文件的路径
[request setFile:res forKey:@"res"]; [queue addOperation:request];
[queue go];

最后是文本的源码,雨松MOMO祝大家学习愉快,不早了,我也得睡觉啦,1点多了。。。

下载地址:http://vdisk.weibo.com/s/accm9

IOS研究院之打开照相机与本地相册选择图片(六)的更多相关文章

  1. IOS研究院之打开照相机与本地相册选择图片

    如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样.最终点击发送将按钮将图片2进制图片上传服务器. 下面我 ...

  2. 微信小程序:从本地相册选择图片或使用相机拍照。

    wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 示例代码: wx.chooseImage({ count: 1, // 默认9 sizeTyp ...

  3. android 开启本地相册选择图片并返回显示

    .java package com.jerry.crop; import java.io.File; import android.app.Activity; import android.conte ...

  4. 微信小程序chooseImage(从本地相册选择图片或使用相机拍照)

    一.使用API wx.chooseImage(OBJECT) var util = require('../../utils/util.js') Page({ data:{ src:"../ ...

  5. HTML5 Plus 拍照或者相册选择图片上传

    HBuilder+HTML5 Plus+MUI实现拍照或者相册选择图片上传,利用HTML5 Plus的Camera.Gallery.IO.Storage和Uploader来实现手机APP拍照或者从相册 ...

  6. [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现

    [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现 今天做Android项目的时候要用到图片选择,要实现拍照获取图片和从相册获取图片,并且要求在获取完之后可以裁剪,试了很多方法之 ...

  7. 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传

    一.Flutter image_picker 实现相机拍照和相册选择   https://pub.dev/packages/image_picker   二.Flutter 上传图片到服务器   ht ...

  8. ng-cordova 手机拍照或从相册选择图片

    1.需求描述 实现一个调用摄像头拍照,或者直接打开本地图库选择照片,然后替换App中图片的功能 2.准备 1) 安装ng-cordova 进入到ionic工程目录,使用bower工具安装, bower ...

  9. 浅谈Android中拍照、从相册选择图片并截图相关知识点

    前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...

随机推荐

  1. jquery 直接调用 wcf,面向服务的SOA架构 ( 第三天)

    所谓万事 具备,只欠东风了!! 接下来就是 wcf 的调用, 首先 在客户端下,随便 写一个 html页面,然后写入如下方法: <script src="scripts/jquery. ...

  2. jQuery整理您的笔记----jQuery开始

    Jquery它是一种高速.简明的JavaScript相框,jQuery设计目标:Write Less,Do More(写更少的代码,做很多其他的事情). 一.Jquery框架优势: 1.轻量级 jQu ...

  3. MVC 5 Web编程2 -- URL映射

    ASP.NET MVC 5 Web编程2 -- URL映射(路由原理) 2015-02-12 08:50 by hangwei, 704 阅读, 5 评论, 收藏, 编辑 本章将讲述ASP.NET M ...

  4. ApacheBench(ab)使用简介

    网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环.只有让服务器处在高压情况下,才能真正体现出软件.硬件等各种设置不当所暴露出的问题. 性能测试工具目前最常见的有以下几种:ab.http_lo ...

  5. jQuery.extend()源码解读

    // extend方法为jQuery对象和init对象的prototype扩展方法// 同时具有独立的扩展普通对象的功能jQuery.extend = jQuery.fn.extend = funct ...

  6. PCL点云库中怎样读取指定的PCD文件,又一次命名,处理后保存到指定目录

    我一直想把处理后的pcd文件重命名,然后放到指定的目录,尝试了好久最终做到了: 比方我想读取  "table_scene_lms400.pcd" 把它进行滤波处理,重命名为 &qu ...

  7. Codeforces Round #FF 446A DZY Loves Sequences

    预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...

  8. PDF解决方案(4)--在线浏览

    相关专题链接 PDF解决方案(1)--文件上传 PDF解决方案(2)--文件转PDF PDF解决方案(3)--PDF转SWF PDF解决方案(4)--在线浏览 前言:上一篇主要提到了PDF在线浏览的各 ...

  9. PhpStorm创建Drupal模块项目开发教程

    在PhpStorm开发工具中,创建Drupal开发项目有两种方式:整合Drupal到现有的项目中和直接创建一个新的Drupal模块. 接下来将展示这两种方式的具体操作! 整合Drupal到现有的项目 ...

  10. Excel和notepad++加之更换

    1. 替换文本的内容Tab长度,粘合剂Excel有效的变革在列. 2. 替换空行.这里用到回车跟换行.回车\r使光标回到最左,换行\n使光标下移一行. 版权声明:本文博客原创文章,博客,未经同意,不得 ...