由于

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:^{}];

}

ios最新调用手机相册选取头像(UIActionSheet过期)的更多相关文章

  1. WebApp调用手机相册或摄像头、拨打电话

    WebApp调用手机相册或摄像头.拨打电话 一.总结 一句话总结:input标签,指定type为file,选择好对应的accept即可.camera——相机,相应的accept为image : cam ...

  2. h5调用手机相册摄像头以及文件夹

    在之前一家公司的时候要做一个app里面有上传头像的功能,当时研究了好久,找到了一篇文章关于h5摄像头以及相册的调用的,所以就解决了这个问题了!!我这里记录一下以便后面有人需要,可以参考一下!!!! 下 ...

  3. web调用手机相册,并实现动态增加图片功能

    注:经测试h5调用相册效果有兼容性问题,安卓仅能调用拍照功能(部分安卓可能会调不起来,所以建议用app原生调用),ios可调起拍照和相册功能. <html xmlns="http:// ...

  4. iOS开发——打开手机相册,获取图片

    1.添加代理UIImagePickerControllerDelegate 2.设置点击跳转事件 - (IBAction)picButton:(UIButton *)sender { NSLog(@& ...

  5. HTML5 调用手机相册和摄像头的方法并上传微信下测试通过

    <input type="file" capture="camera" accept="image/*" id="camer ...

  6. iOS 从手机相册里选取图片

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  7. H5调用相机和相册更换头像

    需求:H5调用手机的相机和相册从而实现更换头像的功能 这个功能是很常用的一个功能,因此做一个记录. 1.在头像img下加一个文件输入框 <input type="file" ...

  8. IOS调用相机相册

    #import "SendViewController.h"  //只能打开,没有加载图片的代码,老代码,供参考 #import <MobileCoreServices/UT ...

  9. ios调用系统相册、相机 显示中文标题、本地化多语言支持

    因为调用系统相册.相机需要显示中文,所以搞了半天才知道是在Project->info->Custom ios Target Properties 添加 Localizations 并加入C ...

随机推荐

  1. Linux文件大小排序

    ls 文件详情 ls   -S /            ###从大到小排序ls   -Sr /          ###从小到大排就加个-r     -r 翻转ls   -aSr /         ...

  2. 零基础学软件测试V2.0

    关于本教程 本系列是在之前的基础上进行了修改更新,原来的内容显得过于简单,但都是重点,这次对于过于简单部分增加了更详细的内容. 目前国内越来越重视软件测试,人才的缺口也是比较大的,为了帮助大家快速的学 ...

  3. 理解Lucene中的Query

    Query是一个接口,它有很多实现类. QueryParser是Query解析器,用于将一个字符串解析为一个Query对象,这个Query对象可能属于TermQuery,也可能属于PhraseQuer ...

  4. C# 发布REST接口地址API服务

    原文地址:https://blog.csdn.net/chinacsharper/article/details/21256569 今天碰巧,用到了淘宝的在线IP地址查询的Rest API,它提供接口 ...

  5. ISCC2014-reverse

    这是我做reverse的题解.在咱逆向之路上的mark一下,,水平有限,大牛见笑. 题目及题解链接:http://pan.baidu.com/s/1gd3k2RL 宗女齐姜 果然是仅仅有50分的难度, ...

  6. struts2每次访问都会创建一个新的session

    1.第一次 项目在测试过程中,突然发现登陆之后再去访问其他菜单时都会提示未登录: 查看日志之后发现是因为很多次请求时都会自动创建一个新的session,这就费解了, 因为之前也没改动什么session ...

  7. spring 中 AOP 功能

    1 PointCut 由 ClassFilter 和 MethodMatcher 构成,通过 ClassFilter 定位到类上,通过 MethodMatcher 定位到方法. 2 Spring 支持 ...

  8. sourcetree和Git的使用教程

    1.简单的用Git管理项目. 2.怎样既要开发又要处理发布出去的版本bug情况. SourceTree是一个免费的Git图形化管理工具,mac下也可以安装. 下载地址:https://www.sour ...

  9. windows live writer 2012 0x80070643

        折腾了两天,windows live writer 安装不成功,最后放弃了,发现一个叫做菊子曰的软件,但是免费版本的,发图片有限制,感觉非常不爽.windows live writer报错如下 ...

  10. C语言 函数参数不确定时 需要用到va_start和va_end函数

    1.在C中,当我们无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表 void foo(...);void foo(parm_list,...); 这种方式和我们以前认识的不大一样,但我 ...