从摄像头或者是从相冊中读取图片。须要通过UIImagePickerController类来实现,在使用UIImagePickerController时,须要是实现以下两个协议

<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

1、从相冊中读取图片

首先要实例化UIImagePickerController对象imagePicker。设置imagePicker的图片来源为UIImagePickerControllerSourceTypePhotoLibrary,表明当前图片的来源为用户的相冊。

以及设置图片是否可被编辑allowsEditing。

#pragma mark - 从用户相冊获取图片
- (void)pickImageFromAlbum
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate =self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing =YES; [self presentModalViewController:imagePicker animated:YES];
}

2、从相冊中读取图片

#pragma mark - 从摄像头获取图片
- (void)pickImageFromCamera
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate =self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing =YES; [self presentModalViewController:imagePicker animated:YES];
}
//打开相机
- (IBAction)touch_photo:(id)sender {
// for iphone
UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType]; }
pickerImage.delegate =self;
pickerImage.allowsEditing =YES;//自己定义照片样式
[self presentViewController:pickerImage animated:YES completion:nil];
}

在用户现则好图片后。会回调选择结束的方法

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
//初始化imageNew为从相机中获得的--
UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//设置image的尺寸
CGSize imagesize = imageNew.size;
imageSize.height =626;
imageSize.width =413;
//对图片大小进行压缩--
imageNew = [self imageWithImage:imageNew scaledToSize:imageSize];
NSData *imageData = UIImageJPEGRepresentation(imageNew,0.00001);
if(m_selectImage==nil)
{
m_selectImage = [UIImage imageWithData:imageData];
NSLog(@"m_selectImage:%@",m_selectImage);
[self.takePhotoButton setImage:m_selectImage forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
return ;
}
}

对图片进行压缩

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize); // Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; // Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context
UIGraphicsEndImageContext(); // Return the new image.
return newImage;
}

将图片保存到Documents文件夹及PNG、JPEG格式相互转换

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1);
} else {
data = UIImagePNGRepresentation(image);
} NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [NSString stringWithString:[self getPath:@"image1"]]; //将图片存储到本地documents
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:dataAttributes:nil]; UIImage *editedImage = [[UIImage alloc] init];
editedImage = image;
CGRect rect = CGRectMake(0, 0, 64, 96);
UIGraphicsBeginImageContext(rect.size);
[editedImage drawInRect:rect];
editedImage = UIGraphicsGetImageFromCurrentImageContext(); UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(10, 10, 64, 96);
[imageButton setImage:editedImage forState:UIControlStateNormal];
[self.view addSubview:imageButton];
[imageButton addTarget:self action:@selector(imageAction:)forControlEvents:UIControlEventTouchUpInside];
[self dismissModalViewControllerAnimated:YES];
} else {
NSLog(@"Media");
}

在上面的方法中不能得到图片的名称及格式。所以须要将其转换成NSData二进制存储

 image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1);
} else {
data = UIImagePNGRepresentation(image);
}
[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:data attributes:nil];  //将图片保存为PNG格式
[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.jpg"] contents:data attributes:nil]; //将图片保存为JPEG格式

【參考资料:http://www.open-open.com/】

iOS图片上传及处理的更多相关文章

  1. IOS 图片上传处理 图片压缩 图片处理

    - (void)initActionSheet { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil dele ...

  2. iOS图片上传1

    这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问题,在这里总结一下. [部分知识] iphone中图像通常存储在4个地方[相册.应用程序包.沙盒.Internet], ...

  3. iOS图片上传后被旋转的问题

    最近用PHP做了一个图片合成程序,前端是通过HTML的file input选取自定图片,POST到php后台调整尺寸后与事先准备好的背景图进行合成. 通过测试发现,上传后的自定图片有的被旋转了,有的是 ...

  4. iOS:图片上传时两种图片压缩方式的比较

    上传图片不全面的想法:把图片保存到本地,然后把图片的路径上传到服务器,最后又由服务器把路径返回,这种方式不具有扩展性,如果用户换了手机,那么新手机的沙盒中就没有服务器返回的图片路径了,此时就无法获取之 ...

  5. iOS图片上传及压缩

    提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用.在这里,我们需要过UIImagePickerController类来和用户交互. 使用UIImagePickerContr ...

  6. 修正ios h5上传图时的图片方向问题

     .ios上传会在exif中带一个 Orientation的属性,这个属性在windows中不会生效,在ios浏览器中会生效,造成图片在windows资源管理器中与ios浏览器中方向不一致  为了用户 ...

  7. iOS 开发之路(WKWebView内嵌HTML5之图片上传) 五

    HTML5页面的图片上传功能在iOS端的实现. 首先,页面上用的是plupload组件,在wkwebview上存在两个坑需要修复才能正常使用. 问题:在webview上点击选择照片/相机拍摄,就会出现 ...

  8. IOS开发-图片上传

    目前IOS端开发,图片上传到服务器分为两种,一种是直接上到服务器,一种是借助第三方储存(减少服务器压力). 一.直接上传到服务器 /** * 代码演示 */ //*******UIImagePNGRe ...

  9. [iOS AFNetworking框架实现HTTP请求、多文件图片上传下载]

    简单的JSON的HTTP传输就不说了,看一个简单的DEMO吧. 主要明白parameters是所填参数,类型是字典型.我把这部分代码封装起来了,以便多次调用.也许写在一起更清楚点. #pragma m ...

随机推荐

  1. Linux进程间通信—管道

    Linux下的进程通信手段基本上是从UNIX平台上的进程通信手段继承而来的.而对UNIX发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间 ...

  2. python的异常机制使用技巧

    1.当你开发的接口被其他应用调用时,响应要及时,但是有些要触发的操作很耗时间. 比如下面需要通过被调用触发的函数create_job_1().但是这个函数执行会比较消耗时间 2.于是,我们可以利用异常 ...

  3. 十个书写Node.js REST API的最佳实践(上)

    收录待用,修改转载已取得腾讯云授权 原文:10 Best Practices for Writing Node.js REST APIs 我们会通过本文介绍下书写Node.js REST API的最佳 ...

  4. 给电脑装完系统之后,发现U盘少了几个G!

    我的U盘是8个G的,有一次用U盘给电脑装完系统,过了几天后再次用的时候发现U盘 突然少了几个G,刚开始不知道怎么回事,然后就格式化U盘,但是格式化之后没有任何 变化. 在网上搜了一下,说是U盘有可能被 ...

  5. RHEL7.0 配置网络IP的三种方法

    导读 RHEL7里面的网卡命名方式从eth0,1,2的方式变成了enoXXXXX的格式. en代表的是enthernet (以太网),o 代表的是onboard (内置),那一串数字是主板的某种索引编 ...

  6. es6中的import,export浏览器已经支持

    直接上代码, 成功测验了es6的新特性 import , export语法. 服务器返回 js文件时,要加上content-type: applicaiton/javascript 这个字段. ind ...

  7. (剑指Offer)面试题39:二叉树的深度

    题目: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 结点的定义如下: struct TreeNode{ int val; ...

  8. (笔试题)数组A中任意两个相邻元素大小相差1,在其中查找某个数。

    题目: 数组A中任意两个相邻元素大小相差1,现给定这样的数组A和目标整数t,找出t在数组A中的位置.如数组:[1,2,3,4,3,4,5,6,5],找到4在数组中的位置. 思路: 很明显,在数组中寻找 ...

  9. C# int与string转化

    1.int-->string ; string s1 = a.ToString(); string s2 = Convert.ToString(a); 2.string -->int &q ...

  10. Android provider中使用sqlite内存数据库

    sqlite是支持内存数据库的,在Android中,我们可以通过provider实现内存数据库操作.内存数据库的优点,访问速度快,但在连接关闭后,数据库自动消失(在android中的表现是,provi ...