码迷,mamicode.com
首页 > 其他好文 > 详细

最基本的添加头像

时间:2015-05-06 16:53:39      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

@interface PTABabyEditBaseVC ()<UIImagePickerControllerDelegate,UIActionSheetDelegate>

@property (nonatomic) UIImagePickerController *imagePickerController;

@end

@implementation PTABabyEditBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
   .......
    
    _headBtn = [[UIButton alloc]initWithFrame:CGRectMake((WIDTH(self.view)-85)/2.0, (HEIGHT(headView)-85)/2.0, 85, 85)];
    [_headBtn.layer setMasksToBounds:YES];
    [_headBtn.layer setCornerRadius:WIDTH(_headBtn)/2.0];
    [_headBtn setBackgroundColor:[UIColor grayColor]];
    [_headBtn addTarget:self action:@selector(toShowCamera) forControlEvents:UIControlEventTouchUpInside];
    [_headBtn setImage:[UIImage imageNamed:@"loginIcon.png"] forState:UIControlStateNormal];
    [headView addSubview:_headBtn];
    
     ........
}

/**
 *  底部弹框
 */
- (void)toShowCamera{
    
    UIActionSheet * actionsheet = [[UIActionSheet alloc] initWithTitle:nil
                                                              delegate:self
                                                     cancelButtonTitle:@"取消"
                                                destructiveButtonTitle:nil
                                                     otherButtonTitles: @"从相册选取",@"拍照", nil];
    [actionsheet showInView:self.view];
}

#pragma mark --
#pragma mark -- UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(buttonIndex==0){
        [self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }else if(buttonIndex==1){
        UIImagePickerController *   imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
        
    }
    
}

- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    imagePickerController.sourceType = sourceType;
    imagePickerController.delegate = self;
    
    self.imagePickerController = imagePickerController;
    [self presentViewController:self.imagePickerController animated:YES completion:nil];
}



#pragma mark --
#pragma mark -- UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        [_headBtn setImage:image forState:UIControlStateNormal];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

 

最基本的添加头像

标签:

原文地址:http://www.cnblogs.com/allanliu/p/4482202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!