iOS调用系统相册,相机上传头像的基本技巧
2016-01-26 17:11
477 查看
//头像点击事件
-(void)photoViewBtn{
UIActionSheet* sheet = [[UIActionSheet alloc
]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从手机相册里选择",
@"拍照", nil];
sheet.tag = 1001;
[sheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//头像
if (actionSheet.tag == 1001) {
if (0 == buttonIndex) {
[self LocalPhoto];
} else if (1 == buttonIndex) {
[self takePhoto];
}
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
//开始拍照
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = sourceType;
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
[self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];
}else{
UIAlertView *al = [[UIAlertView alloc] initWithTitle:nil message:@"模拟器中无法打开照相机,请在真机中使用" delegate:nil cancelButtonTitle:nil
otherButtonTitles:@"确认", nil];
[al show];
}
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
for (UINavigationItem *item in navigationController.navigationBar.subviews) {
if ([item isKindOfClass:[UIButton class]]&&([item.title isEqualToString:@"取消"]||[item.title isEqualToString:@"Cancel"]))
{
UIButton *button = (UIButton *)item;
[button setTitle:@"取消" forState:UIControlStateNormal];
[button setTintColor:[UIColor blackColor]];
}
}
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
// 2.取得的图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
UIImage *img = [UIImage imageWithData:imageData];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
imgView.image = img;
NSData *imgViewData = [self imageWithImage:imgView.image scaledToSize:CGSizeMake(90, 90)];
UIImage *suolue = [UIImage imageWithData:imgViewData]
//上传头像接口在这里调用
[picker dismissViewControllerAnimated:YES completion:nil];
}
}
//将图片缩略到指定大小
- (NSData *)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(newImage, 0.5);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)photoViewBtn{
UIActionSheet* sheet = [[UIActionSheet alloc
]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从手机相册里选择",
@"拍照", nil];
sheet.tag = 1001;
[sheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//头像
if (actionSheet.tag == 1001) {
if (0 == buttonIndex) {
[self LocalPhoto];
} else if (1 == buttonIndex) {
[self takePhoto];
}
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
//开始拍照
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = sourceType;
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
[self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];
}else{
UIAlertView *al = [[UIAlertView alloc] initWithTitle:nil message:@"模拟器中无法打开照相机,请在真机中使用" delegate:nil cancelButtonTitle:nil
otherButtonTitles:@"确认", nil];
[al show];
}
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
for (UINavigationItem *item in navigationController.navigationBar.subviews) {
if ([item isKindOfClass:[UIButton class]]&&([item.title isEqualToString:@"取消"]||[item.title isEqualToString:@"Cancel"]))
{
UIButton *button = (UIButton *)item;
[button setTitle:@"取消" forState:UIControlStateNormal];
[button setTintColor:[UIColor blackColor]];
}
}
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
// 2.取得的图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
UIImage *img = [UIImage imageWithData:imageData];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
imgView.image = img;
NSData *imgViewData = [self imageWithImage:imgView.image scaledToSize:CGSizeMake(90, 90)];
UIImage *suolue = [UIImage imageWithData:imgViewData]
//上传头像接口在这里调用
[picker dismissViewControllerAnimated:YES completion:nil];
}
}
//将图片缩略到指定大小
- (NSData *)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(newImage, 0.5);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
相关文章推荐
- 进击的KFC:IOS开发之格式化日期时间
- iOS性能优化
- IOS监听屏幕状态
- 基础类的DSP/BIOS API调用
- iOS 大学列表检索
- iOS 高效开发-正确的使用枚举(Enum)
- iOS开发系列--视图切换
- iOS崩溃调试的使用和技巧总结
- 使用Xcode和Instruments调试解决iOS内存泄露
- iOS常用正则表达式验证(手机号、密码格式、身份证号等)
- iOS 使用可变参数va_list, 定义一个方法
- ios正则表达式
- IOS深浅拷贝的深入分析
- iOS开发——屏幕尺寸适配
- ios7.2之后的警告汇总
- iOS block反向传值
- iOS黑魔法-Method Swizzling
- iOS - 监听键盘删除键之非代理实现
- [解析苹果官方文档]之[自动布局-view.autoresizingMask]
- iOS 视图控制器生命周期