IOS研究院之打开照相机与本地相册选择图片(二)
035
//把输入框加在视图中
036
[self.view addSubview:_textEditor];
037
038
//下方的图片按钮 点击后呼出菜单 打开摄像机 查找本地相册
039
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"camera" ofType:@"png"]];
040
041
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
042
button.frame = CGRectMake(0, 120, image.size.width, image.size.height);
043
044
[button setImage:image forState:UIControlStateNormal];
045
046
[button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
047
048
//把它也加在视图当中
049
[self.view addSubview:button];
050
051
}
052
053
-(void)openMenu
054
{
055
//在这里呼出下方菜单按钮项
056
myActionSheet = [[UIActionSheet alloc]
057
initWithTitle:nil
058
delegate:self
059
cancelButtonTitle:@"取消"
060
destructiveButtonTitle:nil
061
otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
062
063
[myActionSheet showInView:self.view];
064
[myActionSheet release];
065
066
}
067
068
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
069
{
070
071
//呼出的菜单按钮点击后的响应
072
if (buttonIndex == myActionSheet.cancelButtonIndex)
073
{
074
NSLog(@"取消");
075
}
076
077
switch (buttonIndex)
078
{
079
case 0: //打开照相机拍照
080
[self takePhoto];
081
break;
082
083
case 1: //打开本地相册
084
[self LocalPhoto];
085
break;
086
}
087
}
088
089
//开始拍照
090
-(void)takePhoto
091
{
092
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
093
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
094
{
095
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
096
picker.delegate = self;
097
//设置拍照后的图片可被编辑
098
picker.allowsEditing = YES;
099
picker.sourceType = sourceType;
100
[picker release];
101
[self presentModalViewController:picker animated:YES];
102
}else
103
{
104
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
105
}
106
}
107
108
//打开本地相册
109
-(void)LocalPhoto
110
{
111
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
112
113
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
114
picker.delegate = self;
115
//设置选择后的图片可被编辑
116
picker.allowsEditing = YES;
117
[self presentModalViewController:picker animated:YES];
118
[picker release];
119
}
120
121
//当选择一张图片后进入这里
122
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
123
124
{
125
126
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
127
128