UIImagePicker用法小结


#import "RootViewController.h"

//frameWork中预置了一些跟系统资源相关的常量

#import <MobileCoreServices/MobileCoreServices.h>


@interface RootViewController ()


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    NSArray *array = [NSArray arrayWithObjects:@"相机",@"相册库", nil];

    for (int i=0; i<array.count; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        [btn  setTitle:array[i] forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

        btn.tag = i+100;

        [btn setFrame:CGRectMake(10,74+i*40,300,50)];

        [self.view addSubview:btn];

    }

// Do any additional setup after loading the view.

}


//UIImagePickerController根据不同的type加载不同的资源

//UIImagePickerController 中封装了相机、相册库等系统资源

- (void)loadSourceWithType:(UIImagePickerControllerSourceType)sourceType{

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

    picker.sourceType = sourceType;

    //1 设置代理@property (nonatomic, assign) id<UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate。要实现两个协议。

    picker.delegate = self;

    //是否对相册资源进行自动处理

    picker.allowsEditing = YES;

    //在程序中,对于picker习惯于通过模态化的方式呈现出来

    [self presentViewController:picker animated:YES completion:^{

        

    }];

}


//根据不同的提示信息,创建警告框

- (void)showAlertWithMessage:(NSString *)message{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

    [alert show];

}



- (void)btnClicked:(UIButton *)btn{

    switch (btn.tag) {

        case 100:

        {

           //1 先判断资源是否可用

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                [self loadSourceWithType:UIImagePickerControllerSourceTypeCamera];

            }else{

                [self showAlertWithMessage:@"相机不可用"];

            }

        

        }break;

        case 101:

        {

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

                [self loadSourceWithType:UIImagePickerControllerSourceTypePhotoLibrary];

            }else{

                [self showAlertWithMessage:@"无法调用相册库"];

            }

        

        }break;

        default:

            break;

    }

}


#pragma mark - picker delegate

//点击cancel按钮时,触发此方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    NSLog(@"cancel!");

    [picker dismissViewControllerAnimated:YES completion:^{

        

    }];

}


//选中图片资源时(choose),会触发此方法

//info 中带有选中资源的信息

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

    //先判断资源是否为图片资源(可能是video)

    //获取选中资源的类型

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    //kUTTypeImage 系统预置的图片类型的常量

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

        //取到图片

        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

        self.view.backgroundColor = [UIColor colorWithPatternImage:image];

    }

    [picker dismissViewControllerAnimated:YES completion:^{

        

    }];

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


你可能感兴趣的:(UIImagePicker用法小结)