码迷,mamicode.com
首页 > 移动开发 > 详细

iOS之设置头像(访问系统相册、本地上传)

时间:2016-01-28 15:19:04      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

 

1.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:                               @"设置头像" delegate:self cancelButtonTitle:@"取消"                       destructiveButtonTitle:nil otherButtonTitles:@"选择本地图片",@"拍照", nil];

    [actionSheet showInView:self.view];

 

//2.实现相应代理事件,代理UIActionSheetDelegate,方法如下

 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:                                                       (NSInteger)buttonIndex {

    // 相册 0 拍照 1

    switch (buttonIndex) {

        case 0:

            //从相册中读取

            [self readImageFromAlbum];

            break;

        case 1:

            //拍照

        [self readImageFromCamera];

        break;

        default:

        break;

    }

}

 

//3.实现从相册读取图片功能,代码如下

 

 

//从相册中读取

- (void)readImageFromAlbum {

    //创建对象

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    //(选择类型)表示仅仅从相册中选取照片

    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    //指定代理,因此我们要实现UIImagePickerControllerDelegate,                                                 UINavigationControllerDelegate协议

    imagePicker.delegate = self;

    //设置在相册选完照片后,是否跳到编辑模式进行图片剪裁。(允许用户编辑)

    imagePicker.allowsEditing = YES;

    //显示相册

    [self presentViewController:imagePicker animated:YES completion:nil];

}

 

//4.实现拍照功能

 

- (void)readImageFromCamera {

    if ([UIImagePickerController isSourceTypeAvailable:                                           UIImagePickerControllerSourceTypeCamera]) {

        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];         imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;         imagePicker.delegate = self;

        imagePicker.allowsEditing = YES;

        //允许用户编辑

        [self presentViewController:imagePicker animated:YES completion:nil];

    } else {

        //弹出窗口响应点击事件

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告"                         message:@"未检测到摄像头" delegate:nil cancelButtonTitle:nil                                                 otherButtonTitles:@"确定", nil];

        [alert show];

    }

}

 

//5.图片完成处理后提交,代理方法UIPickerControllerDelegate

 

//图片完成之后处理

- (void)imagePickerController:(UIImagePickerController *)picker        didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

    //image 就是修改后的照片

    //将图片添加到对应的视图上

    [_headImageView setImage:image];

    //结束操作

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

iOS之设置头像(访问系统相册、本地上传)

标签:

原文地址:http://www.cnblogs.com/rglmuselily/p/5166203.html

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