iOS图片上传及处理
从摄像头或者是从相冊中读取图片。须要通过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图片上传及处理的更多相关文章
- IOS 图片上传处理 图片压缩 图片处理
- (void)initActionSheet { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil dele ...
- iOS图片上传1
这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问题,在这里总结一下. [部分知识] iphone中图像通常存储在4个地方[相册.应用程序包.沙盒.Internet], ...
- iOS图片上传后被旋转的问题
最近用PHP做了一个图片合成程序,前端是通过HTML的file input选取自定图片,POST到php后台调整尺寸后与事先准备好的背景图进行合成. 通过测试发现,上传后的自定图片有的被旋转了,有的是 ...
- iOS:图片上传时两种图片压缩方式的比较
上传图片不全面的想法:把图片保存到本地,然后把图片的路径上传到服务器,最后又由服务器把路径返回,这种方式不具有扩展性,如果用户换了手机,那么新手机的沙盒中就没有服务器返回的图片路径了,此时就无法获取之 ...
- iOS图片上传及压缩
提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用.在这里,我们需要过UIImagePickerController类来和用户交互. 使用UIImagePickerContr ...
- 修正ios h5上传图时的图片方向问题
.ios上传会在exif中带一个 Orientation的属性,这个属性在windows中不会生效,在ios浏览器中会生效,造成图片在windows资源管理器中与ios浏览器中方向不一致 为了用户 ...
- iOS 开发之路(WKWebView内嵌HTML5之图片上传) 五
HTML5页面的图片上传功能在iOS端的实现. 首先,页面上用的是plupload组件,在wkwebview上存在两个坑需要修复才能正常使用. 问题:在webview上点击选择照片/相机拍摄,就会出现 ...
- IOS开发-图片上传
目前IOS端开发,图片上传到服务器分为两种,一种是直接上到服务器,一种是借助第三方储存(减少服务器压力). 一.直接上传到服务器 /** * 代码演示 */ //*******UIImagePNGRe ...
- [iOS AFNetworking框架实现HTTP请求、多文件图片上传下载]
简单的JSON的HTTP传输就不说了,看一个简单的DEMO吧. 主要明白parameters是所填参数,类型是字典型.我把这部分代码封装起来了,以便多次调用.也许写在一起更清楚点. #pragma m ...
随机推荐
- JVM:如何分析线程堆栈
英文原文:JVM: How to analyze Thread Dump 在这篇文章里我将教会你如何分析JVM的线程堆栈以及如何从堆栈信息中找出问题的根因.在我看来线程堆栈分析技术是Java EE产品 ...
- sed替换换行符“\n”
linux sed命令,如何替换换行符“\n” 在一次sed使用中,执行命令: sed "s/\n//g" file 1 发现,没起到任何效果. 后来,经查sed官方用户手册,才得 ...
- iOS:使用Github托管自己本地的项目代码方式二(客户端方式: Github Desktop)
管理代码的地方主要有:Github(国外流行).CocoaChina.Cocoa4App.中国开源社区.CSDN.博客园.简书等等..... 前面已经介绍了如何使用命令行和Xcode将本地代码上传到G ...
- 排查VMWare虚拟机的性能问题
Troubleshooting ESX/ESXi virtual machine performance issues (2001003) http://kb.vmware.com/selfservi ...
- RV BaseRecyclerViewAdapterHelper 总结 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 高性能CSS
避免使用@import 有两种方式加载样式文件,一种是link元素,另一种是CSS 2.1加入@import.而在外部的CSS文件中使用@import会使得页面在加载时增加额外的延迟.虽然规则允许在样 ...
- .net维护的一些心得
做了三个月的.net的开发,其实一直在做一个维护加二次开发的工作.现在这个项目告一段落,就此总结下我的所学所感吧.总的来说,.net和JAVA还是有许多地方是不同的,比如.net中的数据绑定问题,已经 ...
- java.lang.IllegalArgumentException:Document base ……does not exist or is not a readable directory错误的解决方案
关于Tomcat的 Document base ……does not exist or is not a readable directory错误 java.lang.IllegalArgumentE ...
- mysql 下 计算 两点 经纬度 之间的距离
公式如下,单位米: 第一点经纬度:lng1 lat1 第二点经纬度:lng2 lat2 round(6378.138*2*asin(sqrt(pow(sin( (lat1*pi()/180-lat2* ...
- window yii2 安装插件 报yiisoft/yii2 2.0.x-dev requires ext-mbstring错
Problem 1 - yiisoft/yii2 2.0.x-dev requires ext-mbstring * -> the requested PHP extens ion mbstri ...