//如果点击头像,弹出上传框
UIActionSheet *sheet = [[UIActionSheetalloc]init];
sheet.delegate =self;
// sheet.title = @"设置头像";
[sheetaddButtonWithTitle:@"拍照"];
[sheetaddButtonWithTitle:@"从相册选择"];
[sheetaddButtonWithTitle:@"取消"];
sheet.cancelButtonIndex =2;
[sheetshowInView:self.view];
实现UIActionSheetDelegate代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc]init];
imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate =self;
imagePicker.allowsEditing =YES;
if (buttonIndex ==0) {
// 调用系统摄像头,拍照
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //UIImagePickerControllerSourceTypeCamera相机模式
picker.sourceType =UIImagePickerControllerSourceTypeCamera;
[selfpresentViewController:pickeranimated:NOcompletion:nil];
}else {
UIAlertView* alertView = [[UIAlertViewalloc]initWithTitle:@""
message:@"当前设备不支持摄像头"
delegate:self
cancelButtonTitle:@"知道了"
otherButtonTitles:nil];
[alertViewshow];
}
}elseif (buttonIndex ==1) {
// 调用系统相册,获取相片
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//UIImagePickerControllerSourceTypePhotoLibrary 照片库模式
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentViewController:pickeranimated:NOcompletion:nil];
}
}
}
实现UIImagePickerDelegate代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 获取选择和裁剪图片
_photoImage = (UIImage *)(info[UIImagePickerControllerEditedImage]);
_photoImage = [_photoImagefixOrientation];
_photoImage = [_photoImagescaleTo:CGSizeMake(120.0,120.0)];
[picker dismissViewControllerAnimated : NO completion : nil ];}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:NOcompletion:nil];
}
-------------------------------------------------------------------------------------------------------------------------------------------
或者这样写
// show action sheet
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照", @"从相册选择照片", nil];
[actionSheet showInView:self.view];
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.写继承代理:
-(void)chooseImage:(id) sender;
@property(retain,nonatomic)UIImageView *imageView;
2.在viewDidLoad中添加操作按钮及显示图像控件
camara begin
UIButton *selectImage = [UIButton buttonWithType:UIButtonTypeRoundedRect];
selectImage.frame = CGRectMake(10, 60, 100, 30);
selectImage.backgroundColor = [UIColor greenColor];
[selectImage setTitle:@"设置头像" forState:UIControlStateNormal];
[selectImage addTarget:self action:@selector(chooseImage:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:selectImage];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100,100, 100)];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.backgroundColor = [UIColor colorWithWhite:.7f alpha:.6f];
[self.view addSubview:self.imageView];
/// camera end
3.完成重载方法
///actionsheet //
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet.tag==255) {
NSUInteger sourceType =0;
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch (buttonIndex) {
case 0:
//cancel
return;
case 1:
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 2:
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
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:YEScompletion:^{}];
}
}
-(void)chooseImage:(id)sender{
UIActionSheet *sheet;
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:selfcancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"相机拍照",@"从相册选择",nil];
}else{
sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:selfcancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil ];
}
sheet.tag = 255;
[sheet showInView:self.view];
}
///image picker delete
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[picker dismissViewControllerAnimated:YES completion:^{}];
UIImage *image =[info objectForKey:UIImagePickerControllerOriginalImage];
[self saveImage:image withName:@"currentImage.png"];
NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents" ] stringByAppendingPathComponent:@"currentImage.png"];
UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
[self.imageView setImage:savedImage];
self.imageView.tag = 100;
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:^{}];
}
-(void)saveImage:(UIImage*)currentImage withName:(NSString*)imageName{
NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.7);
NSString *fullPath =[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName ];
[imageData writeToFile:fullPath atomically:NO];
}