ios开发之调用相机和本地相册
ios调用本地相机和相册, 非常实用的小功能, 一般做上传头像的时候会用到的比较多. 我用的是iPhone5做的测试, 没有出现什么问题. 用真机测试的主要原因是模拟器无法实现拍照功能, 拍照功能只能在真机上调用.下面是我的一些代码, 写的比较粗糙, 但是简单易懂.
// 首先来写一些委托 定义一个全局变量 @interface TestViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate> { UIActionSheet *myActionSheet; }
以上是.h中的代码, 接下来开始写.m中的 至于window创建我就不给列出了
// 首先创建一个button和一个imageView button点击调出拍照或本地相册上传图片方法, imageView负责显示图片 , 把他们都加入到view中 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 100); button.backgroundColor = [UIColor redColor]; [button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; self.img = [[UIImageView alloc] initWithFrame: CGRectMake(0, 220, self.view.frame.size.width, self.view.frame.size.height)]; // 这个imageView我设置的比较大了一点 - - self.img.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_img];
接下来是button的点击方法
-(void)openMenu { //这里面用的是ActionSheet myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"打开相机", @"从相册获取",nil]; [myActionSheet showInView:self.view]; }
然后是ActionSheet的代理方法 和 拍照与本地相册的调用功能实现
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //呼出的菜单按钮点击后的响应 if (buttonIndex == myActionSheet.cancelButtonIndex) { NSLog(@"取消"); } switch (buttonIndex) { case 0: //打开照相机拍照 [self takePhoto]; break; case 1: //打开本地相册 [self LocalPhoto]; break; } } //开始拍照 -(void)takePhoto { UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; //设置拍照后的图片可被编辑 picker.allowsEditing = YES; picker.sourceType = sourceType; [self presentModalViewController:picker animated:YES]; }else { NSLog(@"模拟其中无法打开照相机,请在真机中使用"); } } //打开本地相册 -(void)LocalPhoto { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; //设置选择后的图片可被编辑 picker.allowsEditing = YES; [self presentModalViewController:picker animated:YES]; }
UIImagePickerController的代理方法:
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; //当选择的类型是图片 if ([type isEqualToString:@"public.image"]) { //先把图片转成NSData UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSData *data; if (UIImagePNGRepresentation(image) == nil) { data = UIImageJPEGRepresentation(image, 1.0); } else { data = UIImagePNGRepresentation(image); } //图片保存的路径 //这里将图片放在沙盒的documents文件夹中 NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil]; [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil]; //关闭相册界面 [picker dismissModalViewControllerAnimated:YES]; _img.image = image; } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { NSLog(@"您取消了选择图片"); [picker dismissModalViewControllerAnimated:YES]; }
这样就ok了
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。