
ios调用本地相机和相册,这是非常有用的小功能ios 打开相册和照相机ios 打开相册和照相机,通常在上传头像时使用. 我用iPhone5测试过,没有问题. 使用真实机器测试的主要原因模拟器无法使用拍照功能,只能在真实机器上调用拍照功能. 以下是我的一些代码,用粗糙的语言编写,但简单易懂.
// 首先来写一些委托 定义一个全局变量
@interface TestViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate>
{
UIActionSheet *myActionSheet;
}

上面是.h中的代码,然后开始在.m中编写至于窗口创建,我不会列出它
// 首先创建一个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];

下一步是按钮点击方法
-(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];
}
没关系
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-171754-1.html
题外带句