ios最新调用手机相册选取头像(UIActionSheet过期)
由于
UIActionSheet过期所以可以使用如下调用手机相册
前提不要忘记添加代理如下两个
UIImagePickerControllerDelegate,UINavigationControllerDelegate
还需要去plist文件里面添加相机相册权限否则要崩溃的哟
//更换头像
- (IBAction)changeHeadIM:(id)sender {
//创建UIImagePickerController对象,并设置代理和可编辑
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.editing = YES;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
//创建sheet提示框,提示选择相机还是相册
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"请选择打开方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//相机选项
UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//选择相机时,设置UIImagePickerController对象相关属性
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
//跳转到UIImagePickerController控制器弹出相机
[self presentViewController:imagePicker animated:YES completion:nil];
}];
//相册选项
UIAlertAction * photo = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//选择相册时,设置UIImagePickerController对象相关属性
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//跳转到UIImagePickerController控制器弹出相册
[self presentViewController:imagePicker animated:YES completion:nil];
}];
//取消按钮
UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
//添加各个按钮事件
[alert addAction:camera];
[alert addAction:photo];
[alert addAction:cancel];
//弹出sheet提示框
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{}];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// //原图用image.size.width / image.size.height
// //压缩
// UIGraphicsBeginImageContext(CGSizeMake(800, 600)); //size 为CGSize类型,即你所需要的图片尺寸
// [image drawInRect:CGRectMake(0, 0, 800, 600)];
// UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
float scales = image.size.height / image.size.width;
UIImage *normalImg;
//如果需要改动被压大小,调整scale,而不是kk或aa
if (image.size.width > 600 || image.size.height > 800) {//这里的1000就是scale,所有的都要随着改变
if (scales > 1) {
CGSize newSize = CGSizeMake(600 / scales, 800);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
normalImg = UIGraphicsGetImageFromCurrentImageContext();
}else {
CGSize newSize = CGSizeMake(600 ,800 * scales);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
normalImg = UIGraphicsGetImageFromCurrentImageContext();
}
}else {
normalImg=image;
}
NSData *data = UIImagePNGRepresentation(normalImg);
self.editHeadIM.image = normalImg;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?