您的位置:首页 > 产品设计 > UI/UE

UIImagePickerController的简单使用

2014-09-02 15:26 441 查看
#pragma mark 当点击修改头像按钮的时候,触发的方法

- (IBAction)modifyUserPic:(UIButton *)sender
{
myActionSheet = [[UIActionSheetalloc]initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"打开相机",@"从相册中选择",nil];

[myActionSheet showInView:self.view];

}

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
[self takePhoto];//打开相机
break;
case 1:
[self loadPhoto];//打开本地相册
break;
}
}

//开始拍照
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

picker.delegate = self;

picker.allowsEditing = YES;

picker.sourceType = sourceType;

[self presentViewController:picker animated:YEScompletion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
message:@"无法打开相机,请在真机中调试!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}

}

//打开本地相册
-(void)loadPhoto
{
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

picker.delegate = self;

picker.allowsEditing = YES;

[selfpresentViewController:picker animated:YES completion:^{}];

}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

_userPicImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];

[picker dismissViewControllerAnimated:YES completion:nil];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息