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(表示是否允许应用程序获取框架库内语言)
改中文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
网友评论