【学习ios之路:UI系列】点击更换头像实现从相册读取照片和拍照两种功能

功能如下:

1.点击头像,提示选择更换头像方式①相册 ②照相.

2.点击相册,实现通过读取系统相册,获取图片进行替换.

3.点击照相,通过摄像头照相,进行替换照片.

4.如果摄像头,弹出框警告.

代码如下:

1.通过UIActionSheet对象实现提示功能

   //创建对象 
   UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:
                             @"提示" delegate:self cancelButtonTitle:@"取消" 
                    destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];
    //在视图上展示 
    [actionSheet showInView:self.view];
    [actionSheet release];
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];
    [imagePicker release];
    
}
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];
        [imagePicker release];
    } else {
        //弹出窗口响应点击事件
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" 
                      message:@"未检测到摄像头" delegate:nil cancelButtonTitle:nil 
                                              otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
    }
}
5.图片完成处理后提交,代理方法UIPickerControllerDelegate

//图片完成之后处理

- (void)imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    
   //image 就是修改后的照片
    //将图片添加到对应的视图上
    [button setImage:image forState:UIControlStateNormal];
    //结束操作
    [self dismissViewControllerAnimated:YES completion:nil];
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用\[2\]中提到,使用std::ios::ate打开文件时,文件指针会定位到文件末尾。然而,如果不配合std::ios::in模式,即只使用std::ios::ate和std::ios::out模式打开文件,会清空原文件。所以,std::ios::ate并不能实现追加写入的功能。如果想要实现追加写入,可以使用std::ios::app模式打开文件,这样写入的内容会被追加到文件末尾。 #### 引用[.reference_title] - *1* [C++ 文件读写操作std::ofstream和std::ifstream](https://blog.csdn.net/block999123/article/details/121869208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ofstream之ios::ate,ios::app,ios::in,ios::out](https://blog.csdn.net/cabbage2008/article/details/53307409)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [std::ifstream是C++标准库中的一个输入文件流类,它提供了一组函数来读取文件中的数据](https://blog.csdn.net/m0_46661183/article/details/130769483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值