iOS编程--------UIImagePickerController

//
//  AppDelegate.h
//  UI12_UIImagePickerController
//
//  Created by l on 15/9/16.
//  Copyright (c) 2015年 . All rights reserved.
//

#import 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end





//
//  AppDelegate.m
//  UI12_UIImagePickerController
//
//  Created by l on 15/9/16.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];


    //
    RootViewController *rootVC = [[RootViewController alloc] init];
    self.window.rootViewController = rootVC;

    [rootVC release];



    return YES;
}












//
//  RootViewController.h
//  UI12_UIImagePickerController
//
//  Created by l on 15/9/16.
//  Copyright (c) 2015年 . All rights reserved.
//

#import 

@interface RootViewController : UIViewController

@end





//
//  RootViewController.m
//  UI12_UIImagePickerController
//
//  Created by l on 15/9/16.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

//imageView
@property (nonatomic, retain) UIImageView *imageView;



@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor brownColor];

    //UIImagePickerController 是系统提供的图片选择控制器,用来调用系统相册或者照相机.
    //通常使用模态显示,消失.

    _imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(100, 100, 200, 400))];
    //设置占位图片
    _imageView.image = [UIImage imageNamed:@"playholder.jpg"];

    //添加tap手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerImage:)];

    //添加手势
    [_imageView addGestureRecognizer:tap];

    //打开用户交互
    _imageView.userInteractionEnabled = YES;

    //添加
    [self.view addSubview:_imageView];

    //释放tap
    [tap release];

    // Do any additional setup after loading the view.
}

#pragma mark-- 处理tap事件
- (void)pickerImage:(UITapGestureRecognizer *)tap{

    //需求:点击imageView,创建出来图片选择控制器,然后从系统相册中选择相应的图片,赋值给inageView

    //1.创建一个UIImagePickerController
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    //2.设置选择照片类型
    [picker setSourceType:(UIImagePickerControllerSourceTypePhotoLibrary)];//图片资源库所有图片

    //3.设置代理
    picker.delegate = self;

    //4.模态显示
    [self presentViewController:picker animated:YES completion:nil];




}

#pragma mark-- 图片选择器代理方法
/**
    didFinish 方法 当用户选择一张图片的时候执行
 *  @param picker 图片选择器
    @param info   被选择图片的信息,我们可以通过kvc取值
 */

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
     //1.取出原始图片
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    //2.把图片赋值给imageView
    _imageView.image = image;
    //3.模态消失
    [picker dismissViewControllerAnimated:YES completion:nil];
}



- (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

你可能感兴趣的:([iOS,UI设计],界面,ui,ios,编程)