iOS9 相册的进入及显示图片

来源:互联网 发布:粤菜菜谱大全软件 编辑:程序博客网 时间:2024/04/29 14:21

最近做添加图片功能,发现网络上 demo 大多时间久远,很多一些代理方法已被 APPLE 弃用 ,所以 写个简单的 进入 相册 的 demo,帮助大家////  ViewController.m//  UIImagePickerController////  Created by michael on 15/11/9.//  Copyright © 2015年michael. All rights reserved.//#import "ViewController.h"@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>@property (nonatomic,strong) UIButton *button;@end@implementation ViewController- (void)viewDidLoad {    [superviewDidLoad];    // Do any additional setup after loading the view.    //点击进入相册    [self enterPhotosAlubm];}-(void) enterPhotosAlubm{        //添加按钮及相关属性    _button = [[UIButtonalloc] initWithFrame:CGRectMake(0,20, 200,50)];        [_buttonsetTitle:@"进入相册"forState:(UIControlStateNormal)];        [_buttonsetTitleColor:[UIColorblackColor] forState:(UIControlStateNormal)];        _button.backgroundColor = [UIColoryellowColor];        [self.viewaddSubview:self.button];        //添加点击事件    [_buttonaddTarget:self action:@selector(clickToPicture)forControlEvents:(UIControlEventTouchUpInside)];}//实现点击事件-(void)clickToPicture{        UIImagePickerController *imagePickerController = [[UIImagePickerControlleralloc] init];        imagePickerController.delegate =self;        imagePickerController.allowsEditing =YES;        //点击后推出 相册控制器    [self presentViewController:imagePickerController animated:YEScompletion:nil];        NSLog(@"进入相册");}//实现 UIImagePickerController代理方法-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{        //info 字典中 每个 key所对应的 图片存在 image 对象中,(OriginalImage原始的图片)    UIImage *image = [infoobjectForKey:UIImagePickerControllerOriginalImage];        //自定义 imageView让选中的 image 显示在这个view上    UIImageView *imageView = [[UIImageViewalloc] initWithImage:image];        //imageView 尺寸    imageView.frame =CGRectMake(0,100, self.view.frame.size.width,self.view.frame.size.height - 100);        //picker 图片后隐藏 UIImagePickerController 视图    [picker dismissViewControllerAnimated:YEScompletion:nil];        //最后把 imageView加载到当前 view    [self.view addSubview:imageView];    }//进入相册后 Cancel 键的触发方法-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{        //dismiss 后直接回到前一视图    [self dismissViewControllerAnimated:YES completion:nil];        NSLog(@"cancel to picker");    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end


0 0
原创粉丝点击