用户头像的设置(详细讲解)

简介: 用户头像的设置(详细讲解)

1.挂代理<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

2.点击用户的头像调用下面

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
/**
 *  从手机选择
 *
 */
[alertController addAction:[UIAlertAction actionWithTitle:@"从手机选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"从手机选择");
    //从现有的照片库选择图片, 选择范围仅限最近的相册
    //UIImagePickerControllerSourceTypePhotoLiabrary 从现有的照片库中取出图片
    //UIImagePickerControllerSourceTypePhotoLibrary 从现有的照片库选择图片, 选择范围仅限最近的相册
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *pickerCon = [[UIImagePickerController alloc] init];
        pickerCon.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        pickerCon.allowsEditing = YES;
        pickerCon.delegate = self;
        [self presentViewController:pickerCon animated:YES completion:nil];
    } else
    {
       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的手机没有照相机" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
       [alert show];
       [alert release];
    }
}]];
/**
 *  拍照
 *
 */
[alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"拍照");
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *pickerCon = [[UIImagePickerController alloc] init];
        pickerCon.sourceType = UIImagePickerControllerSourceTypeCamera;
        pickerCon.allowsEditing = YES;
        pickerCon.delegate = self;
        pickerCon.videoQuality = UIImagePickerControllerQualityTypeMedium;
        [self presentViewController:pickerCon animated:YES completion:nil];
    } else
    {
         UIAlertController *alertController1 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的手机没有照相机" preferredStyle:UIAlertControllerStyleAlert];
        [alertController1 addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"返回");
        }]];
        [self presentViewController:alertController1 animated:YES completion:nil];
    }
}]];
/**
 *  取消
 *
 */
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"取消");
}]];
[self presentViewController:alertController animated:YES completion:nil];

3.回调


编辑完回调


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

//UIImagePickerControllerOriginalImage 原始图片
//UIImagePickerControllerEditedImage 编辑过的图片
UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[picker dismissViewControllerAnimated:YES completion:^{
    [_dianji setBackgroundImage:image forState:UIControlStateNormal];
   }];
}

4.相机拍照或者选择图片取消

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
   [picker dismissViewControllerAnimated:YES completion:NULL];
}


提示:1.如果你的下面是英文,那么您需要在info.plist里面添加:Localized resources can be mixed 选择 YES(表示是否允许应用程序获取框架库内语言)


image.png


2.一般用户的头像选择完我们是要进行上传的

获取相机选择后图片的URL名字

NSURL *imageURL = [info valueForKey:@"UIImagePickerControllerReferenceURL"];

如果图片上传就调用

/*
NSURL *imageURL = [info valueForKey:@"UIImagePickerControllerReferenceURL"];
NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&;"];
NSMutableDictionary* pairs = [NSMutableDictionary dictionary];
NSScanner* scanner = [[NSScanner alloc] initWithString:[imageURL query]];
while (![scanner isAtEnd]) {
    NSString* pairString = nil;
    [scanner scanUpToCharactersFromSet:delimiterSet intoString:&pairString];
    [scanner scanCharactersFromSet:delimiterSet intoString:NULL];
    NSArray* kvPair = [pairString componentsSeparatedByString:@"="];
    if (kvPair.count == 2) {
        NSString* key = [[kvPair objectAtIndex:0]
                         stringByReplacingPercentEscapesUsingEncoding:@"UTF-8"];
        NSString* value = [[kvPair objectAtIndex:1]
                           stringByReplacingPercentEscapesUsingEncoding:@"UTF-8"];
        [pairs setObject:value forKey:key];
    }
}
self.imgExt = [pairs objectForKey:@"ext"];
if (self.imgExt == nil || self.imgExt.length < 1) {
    self.imgExt = @"jpg";
}
self.myICO = [self imageWithImageSimple:image scaledToSize:CGSizeMake(440.0, 330.0)];
[self.iconBtn setImage:self.myICO forState:UIControlStateNormal];
[self upImage:@"头像上传中。。。。" upType:5];
 */

补充:有关相册的两个框架

import <MobileCoreServices/UTCoreTypes.h>
 #import <AssetsLibrary/ALAsset.h>

相机的文档1


头像xiaodemo 密码:5ubh

目录
相关文章
|
6月前
|
小程序 JavaScript
小程序授权获取头像
小程序授权获取头像
|
6月前
|
人工智能 Serverless API
想要一个龙年头像,在线等挺急的
想要一个龙年头像,在线等挺急的
889 52
|
6月前
uniapp获取用户头像、昵称
uniapp获取用户头像、昵称
587 0
|
小程序
微信小程序-获取用户头像信息以及修改用户头像
微信小程序-获取用户头像信息以及修改用户头像
547 0
|
小程序 JavaScript 前端开发
微信小程序上传头像和昵称持久化保存
微信小程序上传头像和昵称持久化保存
1214 0
|
缓存 小程序 JavaScript
附解决方案,小程序用户昵称突然变成了“微信用户”,而且头像也显示不了?
附解决方案,小程序用户昵称突然变成了“微信用户”,而且头像也显示不了?
630 0
|
存储 缓存 小程序
小程序获取不到用户头像和昵称返回微信用户问题解决,即小程序授权获取用户头像规则调整的最新解决方案
小程序获取不到用户头像和昵称返回微信用户问题解决,即小程序授权获取用户头像规则调整的最新解决方案
311 0
|
存储 缓存 JavaScript
07设置用户头像的大小 授权获取用户的信息 缓存
07设置用户头像的大小 授权获取用户的信息 缓存
07设置用户头像的大小 授权获取用户的信息 缓存
|
Java Spring
spring boot 中对修改的头像进行更改,同时删除原来的头像,头像是初始头像不删除,信息保存session,实时更新
spring boot 中对修改的头像进行更改,同时删除原来的头像,头像是初始头像不删除,信息保存session,实时更新
547 0