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 ...
随机推荐
- 固定的价格就意味着背叛——《practices of an agile developper》
“对这个项目,我们必须要有固定的报价.虽然我们还不清楚项目的具体情况,但仍要有一个报价.到星期一,我需要整个团队的评估,并且我们必须要在年末交付整个项目.” Venkat & Andy 提出了 ...
- Latex作者单位的写法—AND 首页脚注
IEEE会议的模板 以四个作者为例 正常: 作者单位如果名字较短,可以直接写在作者对应的下面,邮箱可以对应写在再接下来的下面. 一 如果邮箱较长,可以用\thanks{ }命令将其变为脚注.例如: ~ ...
- uva 816 abbott's revenge ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r
- Liunx下文件权限详解
刚接触Linux时对Linux下的文件权限的概念一直很模糊,观念还一直停留在windows下,所以有很多操作一直提示权限不够.为了弄懂文件权限问题我查找了很多资料整理如下,我把这些学习笔记贴出来和大家 ...
- AndroidStudio用Cmake方式编译NDK代码(cmake配置.a库)
1.cmake是什么? CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C+ ...
- 第9章 初识STM32固件库—零死角玩转STM32-F429系列
第9章 初识STM32固件库 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...
- C# 窗体WinForm中动态显示radioButton实例
一个项目中用到的实例,根据数据库查询出待显示的radioButton的个数,显示在一个新的窗口中. //动态显示radioButton public void showRadioButton(int ...
- Json转java对象和List集合
public static void main(String[] args) { // 转换对象 String strJson ="{\"basemenu_id\":\& ...
- http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html
http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html http://www.linuxeden.com/html/softuse ...
- 我追一个处女座的女孩快两个月了,我之前聊得很好,她说过有空call我去看电影,过了一个月她就不理我了,我喜欢她, 我是程序员,百度发不了那么多字。
她刚刚进公司的时候,公司组织去打球,我叫她一起去她也去了,我和她聊了很多,聊得很自然,很开心,如我是哪个学习毕业的 我出来工作多久了等,她也聊了 她自己好多,她现在在读大学,只有周日上一天课那种. 我 ...