恩美第二个APP项目iOS收藏iOS系统框架

iOS头像上传(多图上传)

2016-08-19  本文已影响1215人  RiversMa

刚做了头像上传,下面把整个流程走一遍,希望共同学习,共同进步!

下面我们看demo(本文用UIImagePickerController类上传头像):
首先创建一个全局的UIImagePickerController对象 pickerController 然后初始化对象:

这时候要牵几个代理

<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

然后点击头像开始上传头像,我给头像加了一个手势,点击后跳转到手势方法:

#pragma mark ============手势方法==============
- (void)tapImageHead{
    //初始化pickerController
    pickerController = [[UIImagePickerController alloc] init];
    pickerController.view.backgroundColor = [UIColor orangeColor];
    pickerController.delegate = self;
    pickerController.allowsEditing = YES;
    NSLog(@"点击了头像要修改呦!");
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册中选择", nil];
    actionSheet.frame = CGRectMake(0.f, 0.f, Screen_Width, Screen_Height);
     actionSheet.actionSheetStyle = UIBarStyleBlackOpaque;
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}

这时候会出来下面图片样式:

sheetAlert.png

点击任何一个按钮都会走他的代理方法如下:

#pragma mark ================UIActionSheetDelegate========

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {//相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            NSLog(@"支持相机");
            pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self.viewController presentViewController:pickerController animated:YES completion:nil];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请在设置-->隐私-->相机,中开启本应用的相机访问权限!!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"我知道了", nil];
            [alert show];
        }
    }else  if (buttonIndex == 1){//图片库
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            NSLog(@"支持图库");
            pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self.viewController presentViewController:pickerController animated:YES completion:nil];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请在设置-->隐私-->照片,中开启本应用的相机访问权限!!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"我知道了", nil];
            [alert show];
        }
    }
}
#pragma mark =============UIImagePickerControllerDelegate=======
//用户点击取消退出picker时候调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"%@",picker);
    [picker dismissViewControllerAnimated:YES completion:^{
        }];
}

//这里是用户选中图片(照相后的使用图片或者图库中选中图片)时调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"%s,info == %@",__func__,info);
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [picker dismissViewControllerAnimated:YES completion:^{ 
    }];
    [MBProgressHUD showMessage:@"上传中..." toView:[UIApplication sharedApplication].keyWindow];
    //照片上传
    [self upDateHeadIcon:image];
}

以上就是获取头像(照相或者图库中)的整个流程,下面就是获取到图片后的上传过程,这个要和后台定义好,具体流程见下图:

interfaceHeaderImage2.png

注:红色箭头是一个参数和后台定好的!

注:红色箭头是一个参数和后台定好的!

注:红色箭头是一个参数和后台定好的!

在iOS8.2系统上发现一个bug,就是摄像头第二次调用时,打开是黑屏,没错就是黑屏,然后我测了微信,京东钱包等都一样是黑屏!是黑屏!是黑屏!
然后测试了一下iOS9系统,都是正常的
网上查stackOverflow中说有解决方法,然并卵!
这个我依然没有解决,哪位解决了可以和我说下

多图上传:

整体和头像上传没有什么区别,只是把接口改了一下下面上代码:


- (void)buttonActionPulish{
    //开始发帖
    /*
     uid    true    Long    用户标识
     tid    false   Long    主题帖子标示
     cid    False   Long    回复标示
     comment    true    String  回复内容
     attachment true    byte[]  附件
_selectedPhotos 为图片数组,是image对象数组
     */
    //内容判断
    if (self.reportStateTextView.text.length == 0 ){
        [MBProgressHUD showError:@"内容不能为空"];
        return;
    }
    //上传用户多图接口
    NSString *url = [NSString stringWithFormat:@"%@%@",APP_MGT,AZS_BBS_REPLY_BBS];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *stringUid = [AZSPlistFile readObjectDictionaryFromePlist:USERINFO][@"uid"];
    [params setValue:stringUid forKey:@"uid"];
    [params setObject:self.tID forKey:@"tid"];//主题帖子标示
    if (self.cID != nil) {
        [params setObject:self.cID forKey:@"cid"];//回复贴标识
    }
    [params setObject:self.reportStateTextView.text forKey:@"comment"];//内容
    IMP_BLOCK_SELF(AZSCommunityReplyPostVC);
    [MBProgressHUD showMessage:@"回复中..." toView:self.navigationController.view];
    [AZSDataNetwork uploadWithURL:url params:params fileArray:_selectedPhotos name:@"attachment" fileName:@"fff.jpg" mimeType:@"image/jpeg/png" progress:^(NSProgress *progress) {
        NSLog(@"上传进度%@",progress);
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"上传成功了:%@",responseObject);
        [MBProgressHUD showSuccess:@"回复成功"];
        [MBProgressHUD hideAllHUDsForView: block_self.navigationController.view animated:YES];
        [block_self.navigationController popViewControllerAnimated:YES];
        if (block_self.delegate && [block_self.delegate respondsToSelector:@selector(replySuccess)]) {
            [block_self.delegate replySuccess];  
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"上传失败了:%@",error);
        [MBProgressHUD showError:@"网络错误"];
        [MBProgressHUD hideHUDForView: block_self.navigationController.view animated:YES];
    }]; 
}

针对部分网友提问我下面公开我网络上传图片的具体处理

多图上传.png.png

本文参考:

iOS学习:调用相机和相册,选择图片上传
iOS开发笔记:实现修改头像

上一篇 下一篇

猜你喜欢

热点阅读