相机和相册的调用
来源:互联网 发布:三层网络ping 请求超时 编辑:程序博客网 时间:2024/04/30 02:03
.h文件中添加代理
实现相机,相册的代理方法
UIImagePickerControllerDelegate,UINavigationControllerDelegate,
判断是什么样的设备
-(void)RightBtnAction:(UIBarButtonItem *)sender{ UIActionSheet *sheet; //判断是否可以打开相机,模拟器,ipad的此功能无法使用 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选取", nil]; } else { sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选取", nil]; } sheet.tag = 255; [sheet showInView:self.view];}根据设备选择是相机类型还是相册类型
#pragma mark --ActionSheet---(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (actionSheet.tag == 255) { NSUInteger sourceType = 0; //判断是否支持相机 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { switch (buttonIndex) { case 0: { //取消 break; } case 1: { //相机 sourceType = UIImagePickerControllerSourceTypeCamera; break; } case 2: { //相册 sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; } default: break; } } else { if (buttonIndex == 0) { return; } else { sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } } UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = sourceType; [self presentViewController:imagePickerController animated:YES completion:^{ }]; }}
实现相机,相册的代理方法
#pragma mark---相机照相代理方法--- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{ UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);}#pragma mark--照相照得相片保存到UIImagePickerController--- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if (error != NULL) { UIAlertView *photoSave = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; [photoSave show]; [photoSave dismissWithClickedButtonIndex:0 animated:YES];// [photoSave release]; photoSave = nil; }else { UIAlertView *photoSave = [[UIAlertView alloc] initWithTitle:@"\n\n保存成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; [photoSave show]; [photoSave dismissWithClickedButtonIndex:0 animated:YES];// [photoSave release]; photoSave = nil; [self dismissViewControllerAnimated:YES completion:nil]; }}//相机的代理方法#pragma mark --image picker delegate-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ [picker dismissViewControllerAnimated:YES completion:^{ }]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; /* 此处info 有六个值 * UIImagePickerControllerMediaType; // an NSString UTTypeImage) * UIImagePickerControllerOriginalImage; // a UIImage 原始图片 * UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片 * UIImagePickerControllerCropRect; // an NSValue (CGRect) * UIImagePickerControllerMediaURL; // an NSURL * UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework * UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo */ // 保存图片至本地,方法见下文 [self saveImage:image withName:@"currentImage.png"]; NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"]; UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath]; isFullScreen = NO; switch (count) { case 0: [[dataSource objectAtIndex:0] setImage:savedImage]; break; case 1: [[dataSource objectAtIndex:1] setImage:savedImage]; break; default: [[dataSource objectAtIndex:0] setImage:savedImage]; break; } // [self.imageView setImage:savedImage]; [collectionview reloadData]; self.imageView.tag = 100; count++;}-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [self dismissViewControllerAnimated:YES completion:^{ }];}#pragma mark --保存图片至沙盒-(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]; }
0 0
- 相机和相册的调用
- 调用相册和相机
- 调用相机和相册
- 调用系统的相机和相册
- swift 调用相机和相册的demo
- android调用相机和相册
- android调用相机和相册
- iOS--调用相机和相册
- 调用相机拍摄和相册
- Android调用相机和相册
- 调用系统相册和相机
- UIAction的使用和调用系统相册和相机
- 调用系统相册和调用相机
- iOS调用系统相机和相册的详细用法
- 调用系统相机拍照和选择相册
- 调用系统相册和系统相机拍照
- 笔记35--调用相机和相册
- IOS 如何调用相机和相册
- android编译ffmpeg+x264
- error:无法复制xx:访问被拒绝 请确定磁盘未满或未被写保护 而且文件未被使用
- Android事件处理之多点触摸与手势识别
- 将一个数组中的元素利用另一个数组中的元素进行拆分
- phpMailer 发邮件例子、乱码、发送html内容介绍
- 相机和相册的调用
- hdu-4946 Area of Mushroom 凸包
- C语言字符串操作--删除子串
- 【 D3.js 入门系列 --- 9.5 】 树状图的制作
- 12个方法去创造你人生中的第二次机会
- Binary Tree Traversals hdu 1710 c++
- uva 11971 - Polygon(连续概率问题)
- ss
- 2014 Multi-University Training Contest 8 题解——by xudyh