app often will need to get the picture from the album as a personal avatar, also think of their own to do before, a look at the results, you can directly use UIImagePickerController meet the requirements, it is convenient
UIImagePickerController class to get pictures with video, divided into the following steps:
Initialization UIImagePickerController class;
Set of Data From Examples UIImagePickerController type;
Setting agent;
If you need to do photo editing, then set allowsEditing = YES.
In which a total of three data sources:
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,//照片库模式。该模式显示时会浏览系统照片库的根目录:时刻,所有照片,自拍,连拍快照,屏幕快照等,以tableView类型显示。
UIImagePickerControllerSourceTypeCamera,//相机模式,采用了系统的相机界面
UIImagePickerControllerSourceTypeSavedPhotosAlbum//相机胶卷模式,以时间划分来显示图片
} __TVOS_PROHIBITED;
Before use, we can use isSourceTypeAvailable: determine the source of the media device supports the specified mode.
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];
If you want to record video, you can modify the properties mediaTypes
@property(nonatomic,copy) NSArray<NSString *> *mediaTypes;
// default value is an array containing kUTTypeImage.
You can view the input kUTTypeImage point in xcode into view type;
Above initialized UIImagePickerController, and developed a Delegate, or the realization of several major proxy method
#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 dictionary in a series which will return value 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
After these we find that the interface is in English, then I want to change Chinese
Perform the following two steps
- Localization native development region is provided for in China plist
- If the step operation is not, then the selected info project, and add in the localizations in Chinese;
Note, zh-CHS Simplified Chinese is simple. zh-CHT is pure Traditional Chinese. zh-Hans and zh-CHS corresponding to the same. zh-Hant and zh-CHT correspond to the same.