要先给app设置访问相册的权限:

在项目的Info.plist文件里添加Privacy - Photo Library Usage Description权限

ViewController.h:

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController
{
IBOutlet UIImageView *myImageView; //与ImageView视图关联
} - (IBAction)buttonUp:(id)sender; // 与一个Button关联 @end

ViewController.m:

 #import "ViewController.h"

 @interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>//接口

 @end

 @implementation ViewController

 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)buttonUp:(id)sender {
//初始化UIImagePickerController类
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
//判断数据来源为相册
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//设置代理
picker.delegate = self;
//打开相册
[self presentViewController:picker animated:YES completion:nil];
} //选择完成回调函数
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//获取图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:nil]; myImageView.image = image;
} //用户取消选择
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
} @end

iOS从手机相册选择一张照片并显示 Objective-C的更多相关文章

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

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

  2. iOS 调用系统相册 相机 时,显示中文标题

    解决手机语言已经设置显示中文 在调用系统相册.相机界面 时显示英文问题, 在 info.plist里面添加Localized resources can be mixed YES 表示是否允许应用程序 ...

  3. 【ARK UI】HarmonyOS 从相册选择图片并显示到Image组件上

    ​ 参考资料 [Harmony OS][ARK UI]ETS 上下文基本操作 [Harmony OS][ARK UI]ets使用startAbility或startAbilityForResult方式 ...

  4. MUI - IOS系统,相册选择照片后,点击确定按钮无反应

    MUI框架下使用 plus.gallery.pick 时,选择好照片之后,点击确定按钮无反应(既没报错,也没正确执行成功或失败后的回调方法).这是在做测试时,其中有两台苹果机上出现的bug.做了调试也 ...

  5. Android 拍照或相册选择照片进行显示缩放位图 Demo

    拍照后直接使用 BitmapFactory.decodeStream(...) 进行创建 Bitmap 并显示是有问题的. Bitmap 是个简单对象,它只存储实际像素数据,也就是说,即使原始照片已压 ...

  6. IOS调用相机相册

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

  7. 相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传

    一.点击头像图片 或者按钮 在相册选择照片返回img,网络上传头像要用data表单上传 (1)上传头像属性 // 图片二进制格式 表单上传 @property (nonatomic, strong) ...

  8. IOS研究院之打开照相机与本地相册选择图片

    如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样.最终点击发送将按钮将图片2进制图片上传服务器. 下面我 ...

  9. IOS研究院之打开照相机与本地相册选择图片(六)

    原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址:IOS研究院之打开照相机与本地相册选择图片(六) Hello 大家好 IOS的文章好久都木有更新了,今天更新一篇哈. 这篇文章主要学习 ...

随机推荐

  1. #!/usr/bin/env 脚本解释程序的作用

    the Zimbu programming language http://www.zimbu.org/getting-started -------------------------------- ...

  2. 014 DNS

    解析主机名 Router>en Password: Router#config t Enter configuration commands, one per line.  End with C ...

  3. java 数据结构. 源代码阅读

    Collections工具类里的 Collections.synchronizedList public static <T> List<T> synchronizedList ...

  4. 笔记本能连上WIFI网络,但是无法上网怎么办

    在插网线的台式机上登陆192.168.1.1,点击无线设置,修改一下SSID号,别的什么都不用改.   然后保存,需要重启路由器.重启之后再用笔记本连接新的无线网络即可.

  5. 《软件project》——编码

       编码的目的是使用选定的程序设计语言,把模块的过程描写叙述翻译为用该语言书写的源程序. 源程序应该正确可靠.简明清晰,并且具有较高的效率.在编程的步骤中,要把软件具体设计的表达式翻译成为编程语言的 ...

  6. ZOJ 1806 (小数高精度)

    题意:八进制小数转化成十进制的小数. 0.d1d2d3 ... dk [8] = 0.D1D2D3 ... Dm [10] 例: 0.75 [8] = 7*8^-1+5*8^-2 = ( 5/8 + ...

  7. Map-produce算法两个开源实现

    https://github.com/michaelfairley/mincemeatpy https://github.com/denghongcai/mincemeat-node

  8. html css 仿微信底部自己定义菜单

    近期几个月一直从事微信开发,从刚開始的懵懂渐渐成长了一点. 今天认为微信底部自己定义菜单,假设能在html的页面上也能显示就好了. 记得曾经看过某个网页有类似效果.查找了该网页的css.  ok如今h ...

  9. 修改Android系统的触摸提示音【学习笔记】

    平台信息:内核:Linux version 3.10.0系统:android/android6.0平台:rk3288 作者:庄泽彬(欢迎转载,请注明作者) 邮箱:2760715357@qq.com 本 ...

  10. Code First:Data Anotation (2)

    示例一 本例演示与索引有关的内容,模型: using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAn ...