iOS在更改用户头像并保存至本地沙盒目录中对于UIImagePickerController、UIAlertController的使用
来源:互联网 发布:游戏数据修改器 编辑:程序博客网 时间:2024/06/07 05:19
.h
#import <UIKit/UIKit.h>//获取系统版本号#define IS_iOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? YES : NO)//获取屏幕尺寸#define SW [UIScreen mainScreen].bounds.size.width#define SH [UIScreen mainScreen].bounds.size.height@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>@property (nonatomic, strong)UIButton *iconBtn;@end
.m
- (void)viewDidLoad { [super viewDidLoad]; //初始化Button _iconBtn = [[UIButton alloc]initWithFrame:CGRectMake((SW - 100)/2,(SH / 3) - 100,100,100)]; _iconBtn.backgroundColor =[UIColor redColor]; //加载首先访问本地沙盒是否存在相关图片 NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"]; UIImage *savedImage = [UIImage imageWithContentsOfFile:fullPath]; if (!savedImage) { //默认头像 [_iconBtn setImage:[UIImage imageNamed:@"head"] forState:UIControlStateNormal]; } else { [_iconBtn setImage:savedImage forState:UIControlStateNormal]; } _iconBtn.layer.masksToBounds = YES; _iconBtn.layer.cornerRadius = _iconBtn.frame.size.height / 2; [_iconBtn addTarget:self action:@selector(changeIcon) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_iconBtn];}- (void)changeIcon{ UIAlertController *alertController; __block NSUInteger blockSourceType = 0; // 判断是否支持相机 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //支持访问相机与相册情况 alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击从相册中选取"); //相册 blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = blockSourceType; [self presentViewController:imagePickerController animated:YES completion:nil]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击拍照"); //相机 blockSourceType = UIImagePickerControllerSourceTypeCamera; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = blockSourceType; [self presentViewController:imagePickerController animated:YES completion:nil]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击取消"); // 取消 return; }]]; [self presentViewController:alertController animated:YES completion:nil]; } else { //只支持访问相册情况 alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击从相册中选取"); //相册 blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = blockSourceType; [self presentViewController:imagePickerController animated:YES completion:^{ }]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击取消"); // 取消 return; }]]; [self presentViewController:alertController animated:YES completion:nil]; }}#pragma mark - 选择图片后,回调选择- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ [picker dismissViewControllerAnimated:YES completion:nil]; UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; /* 此处info 有六个可选类型 * UIImagePickerControllerMediaType; // an NSString UTTypeImage) * UIImagePickerControllerOriginalImage; // a UIImage 原始图片 * UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片 * UIImagePickerControllerCropRect; // an NSValue (CGRect) * UIImagePickerControllerMediaURL; // an NSURL * UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework * UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo */ [_iconBtn setImage:image forState:UIControlStateNormal]; [self saveImage:image withName:@"currentImage.png"];}#pragma mark - 保存图片至本地沙盒- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName{ NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.8); // 获取沙盒目录 NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName]; // 将图片写入文件 [imageData writeToFile:fullPath atomically:NO];}
0 0
- iOS在更改用户头像并保存至本地沙盒目录中对于UIImagePickerController、UIAlertController的使用
- UIAlertController和UIImagePickerController的使用
- liferay在自己的portlet中上传更改用户头像
- phpcms 分析用户头像在服务器端的保存与使用
- iOS 在NSObject的子类中使用UIAlertController
- iOS开发——头像设置及本地沙盒保存,圆形头像显示
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- 在iOS 8中使用UIAlertController
- AutoCompleteTextView自定义item布局
- IBM PowerKVM
- Javascript和Ajax中文乱码吐血版解决方案
- mysq windows和linux安装以及常见命令文档
- Java基础collection
- iOS在更改用户头像并保存至本地沙盒目录中对于UIImagePickerController、UIAlertController的使用
- mongodb查看数据库和表的信息
- VSCode配置C++编写环境
- 递归3_计算多个数字
- MySQL数据类型之CHAR与VARCHAR及row size max=65535bytes
- 对集合类Vector和Enumeration的应用
- VLC设置串流的TTL值
- ios CAShapeLayer和UIBezierPath
- 设计模式