iOS 从相册加载图片到imageview

本文介绍如何在iOS应用中实现从相册选取图片并显示的功能。通过创建一个按钮和图片视图,设置按钮的动作来打开照片选择器,并在用户选择图片后将其显示在图片视图上。

1.新建project,在storyboard中拖入一个button和一个image view

2.在.m文件中给button添加action,

@implementation ViewController


- (IBAction)button:(id)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    picker.allowsEditing = YES;

    [self presentViewController:picker animated:YES completion:NULL];

    

    

    //处理完毕,回到个人信息页面

    [picker dismissViewControllerAnimated:YES completion:NULL];

}


@end

3.在.m文件中给image view 添加property。

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *myimage;

@end



4.在.m文件中添加代码

@implementation ViewController


#pragma mark - image picker delegte


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info


{    

    [picker dismissViewControllerAnimated:YES completion:^{}];

    

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [self.myimage setImage:image];  

}


@end

5.运行测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值