iOS图片处理(一)————调用系统相机和相册获取图片,给相机添加自定义覆盖物
2014-02-18 12:47
721 查看
首先,照相或从相册选择照片需要使用UIImagePickerController,使用时需要添加两个协议
从相机获取图片的方式:
从相册获取图片的方式:
保存选择的图片并显示在控件上:
取消操作时调用的方法:
DEMO下载地址: http://download.csdn.net/download/u011918080/6932721
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *imageScroll; @property (weak, nonatomic) IBOutlet UIImageView *photoImage; @end
从相机获取图片的方式:
//从相机获取图片 - (IBAction)photoFromCamera:(id)sender { if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;//设置类型为相机 UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化 picker.delegate = self;//设置代理 picker.allowsEditing = YES;//设置照片可编辑 picker.sourceType = sourceType; //picker.showsCameraControls = NO;//默认为YES //创建叠加层 UIView *overLayView=[[UIView alloc]initWithFrame:CGRectMake(0, 120, 320, 254)]; //取景器的背景图片,该图片中间挖掉了一块变成透明,用来显示摄像头获取的图片; UIImage *overLayImag=[UIImage imageNamed:@"zhaoxiangdingwei.png"]; UIImageView *bgImageView=[[UIImageView alloc]initWithImage:overLayImag]; [overLayView addSubview:bgImageView]; picker.cameraOverlayView=overLayView; picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;//选择前置摄像头或后置摄像头 [self presentViewController:picker animated:YES completion:^{ }]; } else { NSLog(@"该设备无相机"); } }
从相册获取图片的方式:
//从相册获取图片 - (IBAction)photoFromAlbum:(id)sender { UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType]; } pickerImage.delegate = self; pickerImage.allowsEditing = NO; [self presentViewController:pickerImage animated:YES completion:^{ }]; }从相册或相机选择图片之后的操作:
//从相册选择图片后操作 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{ }]; //NSLog(@"%@",info); //保存原始图片 UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self saveImage:image withName:@"currentImage.png"]; }
保存选择的图片并显示在控件上:
//保存图片 - (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName { NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5); // 获取沙盒目录 NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName]; // 将图片写入文件 [imageData writeToFile:fullPath atomically:NO]; //将选择的图片显示出来 [self.photoImage setImage:[UIImage imageWithContentsOfFile:fullPath]]; //将图片保存到disk UIImageWriteToSavedPhotosAlbum(currentImage, nil, nil, nil); }
取消操作时调用的方法:
//取消操作时调用 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:^{ }]; }
DEMO下载地址: http://download.csdn.net/download/u011918080/6932721
相关文章推荐
- iOS图片处理(一)调用系统相机和相册获取图片,给相机添加自定义覆盖物
- iOS开发swift如何调用系统相册和相机获取图片设置用户头像
- iOS 调用相机 ,相册获取图片 并裁剪为正方形
- iOS调用系统相册和相机选择图片
- Android调用系统相机、自定义相机、处理大图片
- Android图片系列(1)-------调用系统相册与相机获取图片
- iOS获取相册/相机图片-------自定义获取图片小控件
- Android调用系统相机和相册添加图片到
- android 调用系统相册选取照片或者打开相机拍照获取图片
- 调用系统相机拍照获取图片或者从相册中调用本地图片显示问题
- Android 拍照以及一些常用的处理,例如将图片显示到相册(包含了安卓系统6.0以上调用相机的处理)
- android 调用系统相册选取照片或者打开相机拍照获取图片
- Android中调用系统相机、系统相册来获取图片,并裁剪图片。
- android实例之——调用android系统相机、系统相册,并对得到的图片进行处理
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- Android调用系统相机、自定义相机、处理大图片
- Android开发基础 调用相机 系统相册(并对图片进行压缩处理)
- 调用系统相机/相册获取图片的完美解决方案
- Anroid调用系统相机或相册获取图片
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController