iOS调用相机,相册,上传头像

一、新建工程

二、拖控件,创建映射

三、在.h中加入delegate

  1. @interface ViewController : UIViewController

复制代码

四、实现按钮事件

  1. -(IBAction)chooseImage:(id)sender {
  2. UIActionSheet *sheet;
  3. // 判断是否支持相机
  4. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  5. {
  6. sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
  7. }
  8. else {
  9. sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
  10. }
  11. sheet.tag = 255;
  12. [sheet showInView:self.view];
  13. }

复制代码

五、实现actionSheet delegate事件

  1. -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  2. {
  3. if (actionSheet.tag == 255) {
  4. NSUInteger sourceType = 0;
  5. // 判断是否支持相机
  6. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  7. switch (buttonIndex) {
  8. case 0:
  9. // 取消
  10. return;
  11. case 1:
  12. // 相机
  13. sourceType = UIImagePickerControllerSourceTypeCamera;
  14. break;
  15. case 2:
  16. // 相册
  17. sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  18. break;
  19. }
  20. }
  21. else {
  22. if (buttonIndex == 0) {
  23. return;
  24. } else {
  25. sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  26. }
  27. }
  28. // 跳转到相机或相册页面
  29. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  30. imagePickerController.delegate = self;
  31. imagePickerController.allowsEditing = YES;
  32. imagePickerController.sourceType = sourceType;
  33. [self presentViewController:imagePickerController animated:YES completion:^{}];
  34. [imagePickerController release];
  35. }
  36. }

复制代码

六、实现ImagePicker delegate 事件

  1. #pragma mark - image picker delegte
  2. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  3. {
  4. [picker dismissViewControllerAnimated:YES completion:^{}];
  5. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  6. /* 此处info 有六个值
  7. * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
  8. * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
  9. * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
  10. * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
  11. * UIImagePickerControllerMediaURL;       // an NSURL
  12. * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
  13. * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
  14. */
  15. // 保存图片至本地,方法见下文
  16. [self saveImage:image withName:@"currentImage.png"];
  17. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
  18. UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
  19. isFullScreen = NO;
  20. [self.imageView setImage:savedImage];
  21. self.imageView.tag = 100;
  22. }
  23. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  24. {
  25. [self dismissViewControllerAnimated:YES completion:^{}];
  26. }

复制代码

七、保存图片
高保真压缩图片方法

  1. NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality
  2. )

复制代码

此方法可将图片压缩,但是图片质量基本不变,第二个参数即图片质量参数。

  1. #pragma mark - 保存图片至沙盒
  2. - (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
  3. {
  4. NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
  5. // 获取沙盒目录
  6. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
  7. // 将图片写入文件
  8. [imageData writeToFile:fullPath atomically:NO];
  9. }

复制代码

八、实现点击图片预览功能,滑动放大缩小,带动画

  1. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  2. {
  3. isFullScreen = !isFullScreen;
  4. UITouch *touch = [touches anyObject];
  5. CGPoint touchPoint = [touch locationInView:self.view];
  6. CGPoint imagePoint = self.imageView.frame.origin;
  7. //touchPoint.x ,touchPoint.y 就是触点的坐标
  8. // 触点在imageView内,点击imageView时 放大,再次点击时缩小
  9. if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width >=touchPoint.x && imagePoint.y <=  touchPoint.y && imagePoint.y+self.imageView.frame.size.height >= touchPoint.y)
  10. {
  11. // 设置图片放大动画
  12. [UIView beginAnimations:nil context:nil];
  13. // 动画时间
  14. [UIView setAnimationDuration:1];
  15. if (isFullScreen) {
  16. // 放大尺寸
  17. self.imageView.frame = CGRectMake(0, 0, 320, 480);
  18. }
  19. else {
  20. // 缩小尺寸
  21. self.imageView.frame = CGRectMake(50, 65, 90, 115);
  22. }
  23. // commit动画
  24. [UIView commitAnimations];
  25. }
  26. }

复制代码

九、上传图片,使用ASIhttpRequest类库实现,由于本文重点不是网络请求,故不对ASIHttpRequest详细讲述,只贴出部分代码

  1. ASIFormDataRequest *requestReport  = [[ASIFormDataRequest alloc] initWithURL:服务器地址];
  2. NSString *Path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
  3. [requestReport setFile:Path forKey:@"picturepath"];
  4. [requestReport buildPostBody];
  5. requestReport.delegate = self;
  6. [requestReport startAsynchronous];

复制代码

时间: 2024-10-07 05:30:41

iOS调用相机,相册,上传头像的相关文章

调用相机并且上传照片的方法

#import "ViewController.h" #import "AFNetworking.h" @interface ViewController () <UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *imageView;

IOS调用相机相册

#import "SendViewController.h"  //只能打开,没有加载图片的代码,老代码,供参考 #import <MobileCoreServices/UTCoreTypes.h> @interface SendViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate> -(IBAction)sel

iOS开发类似微信上传头像小操作Demo

效果图: 代码: 图片选择器前面的tablvew里的东西 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 图片选择器 UIImagePickerController *imgPC = [[UIImagePickerController alloc] init]; //设置代理 imgPC.delegate = self; //允许编辑图片 imgPC.

相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传

一.点击头像图片 或者按钮 在相册选择照片返回img,网络上传头像要用data表单上传 (1)上传头像属性 // 图片二进制格式 表单上传 @property (nonatomic, strong) NSData *imageWithData; (2)头像点击事件 - (void)headImageEvent{ NSLog(@"上传头像"); [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) { self.heade

IOS上传头像

//1   ASI post传图片 NSURL *url=[NSURL URLWithString:[BASEURL stringByAppendingString:@"/user/post_face.php"]]; ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url]; request.delegate=self; request.timeOutSeconds=120; [email protected

swift上传头像

很久没有写博客了,今天特地写了这个,也是一边仿照别人写的demo,注释部分都是需要的.需要的同学可以参考一下. @IBAction func headImageBtnPage(){  //上传头像 //        let actionSheet = UIAlertController(title: "上传头像", message: nil, preferredStyle: .ActionSheet) //        let cancelBtn = UIAlertAction(t

Android基础之——startActivityForResult启动界面并返回数据,上传头像

在android应用的开发过程中,经常会出现启动一个界面后填写部分内容后带着数据返回启动前的界面,最典型的应用就是登录过程.在很多应用程序的模块中,都有"我的"这个模块,在未登录状态下点击其中的某一项,就会弹出登录界面,登录完成后回到我的界面,会显示一些登录后的数据,这个功能的实现就要用到startActivityForResult. 下面通过一个小demo来说明一下startActivityForResult的使用,以及在实际开发中的一些应用. demo的效果图如下: 主界面布局:

Jcrop+uploadify+php实现上传头像预览裁剪

最近由于项目需要,所以做了一个上传头像预览并且可以预览裁剪的功能,大概思路是上传的图片先保存到服务器,然后通过ajax从服务器获取到图片信息,再利用Jcrop插件进行裁剪,之后通过PHP获取到的四个裁切点的坐标进行裁剪. 首先看一下uploadify上传插件的API: uploader : uploadify.swf 文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,点击后淡出打开文件对话框,默认值:uploadify.swf.script :   后台处理程序的相对路径 .默认值:

asp.net mvc上传头像加剪裁功能

正好项目用到上传+剪裁功能,发上来便于以后使用. 我不能告诉你们其实是从博客园扒的前台代码,哈哈. 前端是jquery+fineuploader+jquery.Jcrop 后台是asp.net mvc 4 核心的js调用代码是crop.js和helper文件夹下的ImgHandler.cs 效果图 前台代码 <link href="~/Content/fineuploader.css" rel="stylesheet" /> <link href=