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

}

时间: 2025-01-15 19:26:02

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

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

注:经测试h5调用相册效果有兼容性问题,安卓仅能调用拍照功能(部分安卓可能会调不起来,所以建议用app原生调用),ios可调起拍照和相册功能. <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无

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

1.添加代理UIImagePickerControllerDelegate 2.设置点击跳转事件 - (IBAction)picButton:(UIButton *)sender { NSLog(@"我的相册"); if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ //a.初始化相册拾取器 UIImagePickerController *

iOS中调用系统相册以及使用系统相册的照片

#import "ViewController.h" @interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate> @property (nonatomic,strong)UIImageView *headImageView; @end @implementation ViewController - (void)viewDidLoad { [supe

调用手机相册和文档

<input type="file" capture="camera" accept="image/" multiple="multiple"/> <input type="file"capture="camcorder" accept="video/"/> <input type="file" capture=&qu

iOS 图片保存手机相册

UIImageWriteToSavedPhotosAlbum(app.erweiImg, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { UIAlertView *alert; if (error

iOS 从手机相册里选取图片

#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end

兼容安卓和苹果移动端就input调起手机相册和相机

以下这么写的话,苹果手机可以调起相机和相册功能,但是安卓手机只能调起相册: <input id="upLicense" onchange="preview(this,0)" type="file" name="upLicense" > <input id="upLicense" onchange="preview(this,0)" type="file&quo

Swift开发之调用系统相册

对于iOS 中调用系统相册的功能,我想大家都比较熟悉了,但是Swift语言调用可能很多伙伴们不是很清楚,毕竟Swift是一门新语言,所以语法和实现方法可能不是很清楚,所以今天做了一个demo,大家可以做一下参考. // //  ViewController.swift //  iOS // //  Created by 悦兑科技 on 15/1/12. //  Copyright (c) 2015年 BSY. All rights reserved. // import UIKit class

iOS开发之调用手机摄像头和相册

//按钮的点击方法- (void)catchImage { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择" message:@"选取照片" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相机&