您的位置:首页 > 产品设计 > UI/UE

iOS 学习笔记-个人头像获取UIImagePickerController使用

2017-03-06 14:05 549 查看
app中经常会需要从相册中获取图片来作为个人头像,之前还想着自己来做,结果一看,使用UIImagePickerController直接就可以达到要求,很方便

用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:

初始化UIImagePickerController 类;

设置UIImagePickerController 实例的数据来源类型;

设置设置代理;

如果需要做图片修改的话设置allowsEditing = YES。

其中数据来源一共有三种:

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,//照片库模式。该模式显示时会浏览系统照片库的根目录:时刻,所有照片,自拍,连拍快照,屏幕快照等,以tableView类型显示。
UIImagePickerControllerSourceTypeCamera,//相机模式,采用了系统的相机界面
UIImagePickerControllerSourceTypeSavedPhotosAlbum//相机胶卷模式,以时间划分来显示图片
} __TVOS_PROHIBITED;


使用前我们可以使用isSourceTypeAvailable:判断设备支持指定的媒体来源模式。

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@"支持相机");
}
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
NSLog(@"支持图库");
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
NSLog(@"支持相片库");
}


UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
//是否在确定页面出现裁剪框,进行裁剪
pickerController.allowsEditing = YES;

pickerController.delegate = self;

UIAlertController *sheetCtr = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//拍照
UIAlertAction *cameraAc = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//设置相册呈现的样式
pickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;//图片分组列表样式
//使用模态呈现相册
[self.navigationController presentViewController:pickerController animated:YES completion:^{
//
}];
}];
[sheetCtr addAction:cameraAc];
}

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//照片
UIAlertAction *photoAc = [UIAlertAction actionWithTitle:@"从手机相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//设置相册呈现的样式
pickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;//图片分组列表样式
//使用模态呈现相册
[self.navigationController presentViewController:pickerController animated:YES completion:^{

}];
}];

[sheetCtr addAction:photoAc];
}

//取消
UIAlertAction *cancleAc = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}];

[sheetCtr addAction:cancleAc];

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


如果想要可以录像,可以修改mediaTypes属性

@property(nonatomic,copy)      NSArray<NSString *>                   *mediaTypes;
// default value is an array containing kUTTypeImage.


你可以查看在xcode里输入 kUTTypeImage 点进去查看类型;

上面初始化了UIImagePickerController,并制定了Delegate,最主要还是实现几个代理方法

#pragma mark - UIImagePickerController 代理方法
//选择照片完成之后的代理方法

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

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

NSLog(@"info :%@",info);

NSData *data;

if ([mediaType isEqualToString:@"public.image"]){
UIImage *originImage = [info objectForKey:UIImagePickerControllerEditedImage];

//图片压缩,如不需要可以去掉
UIImage *scaleImage = [self scaleImage:originImage toScale:0.3];

//以下这两步都是比较耗时的操作,最好开一个HUD提示用户,这样体验会好些,不至于阻塞界面
if (UIImagePNGRepresentation(scaleImage) == nil) {
//将图片转换为JPG格式的二进制数据
data = UIImageJPEGRepresentation(scaleImage, 1);
//UIImageJPEGRepresentation(,)通过设置第二个参数值,能够压缩图片,如设置为0.5,则生成的NSData越小.
} else {
//将图片转换为PNG格式的二进制数据
data = UIImagePNGRepresentation(scaleImage);
}

//将二进制数据生成UIImage
UIImage *image = [UIImage imageWithData:data];

//然后再传到服务器上

}

[self.navigationController dismissViewControllerAnimated:YES completion:nil];

}

//点击取消按钮所执行的方法

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];

}

#pragma mark- 缩放图片
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}


didFinishPickingMediaWithInfo: 中 info字典里面会返回一系列keyValue值

NSString *const UIImagePickerControllerMediaType;//指定用户选择的媒体类型
NSString *const UIImagePickerControllerOriginalImage;//原始图片
NSString *const UIImagePickerControllerEditedImage;//修改后的图片
NSString *const UIImagePickerControllerCropRect;//裁剪尺寸
NSString *const UIImagePickerControllerMediaURL;//媒体的URL
NSString *const UIImagePickerControllerReferenceURL;//原件的URL
NSString *const UIImagePickerControllerMediaMetadata;//当来数据来源是照相机的时候这个值才有效
NSString *const UIImagePickerControllerLivePhoto;  // a PHLivePhoto


完成这些之后我们发现界面是英文的,那我想要改成中文

执行以下两步即可

在plist中设置Localization native development region为China



如果上步操作不行,就再选定project的info,在localizations中添加中文;

注意,zh-CHS 是单纯的简体中文。zh-CHT 是单纯的繁体中文。zh-Hans和zh-CHS相同相对应。zh-Hant和zh-CHT相同相对应。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片 ios 视频