从摄像头或者是从相冊中读取图片。须要通过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. Computer Vision Tutorials from Conferences (3) -- CVPR

    CVPR 2013 (http://www.pamitc.org/cvpr13/tutorials.php) Foundations of Spatial SpectroscopyJames Cogg ...

  2. Android程序调试

    1. 使用Log.d方法输出Debug日志信息. Log.d方法用来输出DEBUG故障日志信息,该方法有两种重载形式,其中开发人员经常用到的重载形式语法如下: public static int d( ...

  3. http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html

    http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html http://www.linuxeden.com/html/softuse ...

  4. Java import javax.servlet 出错

    Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...

  5. [Angular-Scaled web] 4. Using ui-router's named views

    In the previous post, we use $stateProvider deriect to root url and using MainCtrl, categories.tmpl. ...

  6. systemctl使用

    systemctl start httpd.service 这会启动httpd服务,就我们而言,Apache HTTP服务器. 要停掉它,需要以root身份使用该命令: systemctl stop ...

  7. T-SQL 之 运算符

    1.算术运算符 [1] +:加 [2] -:减 [3] *:乘 [4] /:除 [5] %:模除取余 2.位运算符 [1] &(与,and): 按位逻辑与运算 [2] |(或,or): 按位逻 ...

  8. iOS XMPPFramework 环境配置

    iOS XMPPFramework 环境配置 1:下载 iOS XMPPFramework https://github.com/robbiehanson/XMPPFramework 2:下载解压zi ...

  9. 使用poll处理任意数目个客户的单进程程序

    将http://www.cnblogs.com/nufangrensheng/p/3590002.html中的select改用poll. int main(int argc, char **argv) ...

  10. leetCode(29):Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...