美文网首页
iOS 换头像

iOS 换头像

作者: 不说谎的匹诺曹Y | 来源:发表于2017-06-30 11:38 被阅读0次

 换头像 必须添加info.plist 的两个权限  摄像头和相册

添加协议  添加成员变量

<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

{

UITableView *tableV;

UIImagePickerController *pickerC;

UIImageView *imageV;

UIImage *myImage;

}

@property (nonatomic,strong)NSMutableArray *Marr;

@end

------------------------       初始化图片选择器    ----------------------

pickerC = [[UIImagePickerController alloc]init];

pickerC.delegate = self;

pickerC.allowsEditing = YES;

 -----------------------       实现代理方法     -----------------------

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.Marr.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellID = @"cyy";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];        if (!cell) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];    }    cell.textLabel.text = self.Marr[indexPath.row];            NSInteger Tag = 10;    imageV = [cell.contentView viewWithTag:Tag];        if (!imageV) {        imageV = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 80, 10, 60, 60)];                imageV.backgroundColor = [UIColor redColor];        imageV.image = [UIImage imageNamed:@"1"];                [cell.contentView addSubview:imageV];    }    imageV.image = myImage;    return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{        if(indexPath.row == 0){                UIAlertController *alert = [[UIAlertController alloc]init];                UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"取消");        }];                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"打开相机");                        [self openXJ];                    }];                UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"打开相册");                        [self openXC];        }];                [alert addAction:action];        [alert addAction:action1];        [alert addAction:action2];                [self presentViewController:alert animated:YES completion:nil];    }    }- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary*)editingInfo NS_DEPRECATED_IOS(2_0, 3_0){

if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

}

myImage = image;

[tableV reloadData];

[self dismissViewControllerAnimated:YES completion:nil];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

[self dismissViewControllerAnimated:YES completion:nil];

}

-(void)openXJ{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

pickerC.sourceType = UIImagePickerControllerSourceTypeCamera;

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

}else{

NSLog(@"打开失败");

}

}

-(void)openXC{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

pickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

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

}else{

NSLog(@"打开失败");

}

}

相关文章

  • iOS 换头像

    换头像 必须添加info.plist 的两个权限 摄像头和相册 添加协议 添加成员变量 {UITableView ...

  • 换头像

    你的头像 是那个 戴帽子的美男 我已经习惯 我@你 却找不到你 从头拉到尾 再从尾拉到头 我一个一个图像查看 没有...

  • 换头像

    不错,生活是很苦 给自己换一个欢快一点的头像 给微信,给QQ 给博客都彩彩妆 打今儿起,微笑着过活 打今儿起,积极...

  • 换头像

    心血来潮换个头像 本想换掉自己心情的沮丧 来个高端大气上档次的模样 让心情也跟着头像一起敞亮 驱走阴霾晒晒太阳 经...

  • 换头像

    微信头像不习惯用自己人像,一因长得没特点,二因不自恋,就用了一个风景照。但家人说头像看着太乱,要求更换,这才发现选...

  • 换头像

    从今天起,头像更换如下图,关注的小伙伴不要走丢哦!

  • 换头像

    今天把头像给换了换成我最爱的王大仁女士! 我真的好喜欢他呀!我真的好想成为他这样的人啊!自信!大方优雅美丽!反正我...

  • 换头像

    自从用上微信,一直在用一张心如止水般的头像。前日被提醒说头像太老了,才后知后觉地发现这个头像就没换过。 于是自己也...

  • 换头像

    最近我换了一个微信头像,大学时候的照片 。因为这几年的记忆我想将它们尘封。

  • 换头像

    换头像 狄安娜 随着心情,换了几次头像,总有人跳出来反对,强烈表达对第一个头像的喜爱,可是我已经忘记第一个...

网友评论

      本文标题:iOS 换头像

      本文链接:https://www.haomeiwen.com/subject/yevbcxtx.html