iOS开发相册,照相机的调用

相册、相机UIImagePickerController  相册、相机的调用都是用此类,sourceType不一致协议UINavigationControllerDelegateUIImagePickerControllerDelegate当你选择照片或拍照后回传图片信息imagePickerController:didFinishPickingMediaWithInfo:当你点击取消键时回调(若实现此方法,需手动返回上一界面)imagePickerControllerDidCancel:      

  例子:将从系统相框选出照片作为屏幕背景建一个工具类.m中#import#import@interface ImageTool : NSObject//实现一个方法压缩图片+(UIImage *)resizeImagetoSize:(CGSize)size WithImage:(UIImage*)image;@end工具类.h中#import "ImageTool.h"@implementation ImageTool//实现一个类方法压 缩图片+(UIImage *)resizeImagetoSize:(CGSize)size WithImage:(UIImage*)image{//    重绘    UIGraphicsBeginImageContext(size);    //    获取上下文对象    CGContextRef context=UIGraphicsGetCurrentContext();    //  缩放        CGContextTranslateCTM(context, 0, size.height);    CGContextScaleCTM(context, 1,-1);    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), image.CGImage);//    得到UIImage    UIImage *resultImage=UIGraphicsGetImageFromCurrentImageContext();    //    结束绘图    UIGraphicsEndImageContext();            return resultImage;}@endVC类.h中#import "ViewController.h"#import "ImageTool.h"//定义屏幕宽高的宏#define HEIGHT self.view.frame.size.height#define WIDTH self.view.frame.size.width@interface ViewController ()@end

@implementation ViewController

{

UIActionSheet *_actionSheet;

UIImageView * _background;

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self creatBackground];

[self creatActionSheet];

[self creatButton];

}

//创建图片背景

-(void)creatBackground

{

_background =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

_background.backgroundColor=[UIColor yellowColor];

[self.view addSubview:_background];

}

//创建

-(void)creatActionSheet

{

_actionSheet=[[UIActionSheet alloc]initWithTitle:@"请选择功能" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"相机", nil];

}

#pragma mark 实现ActionSheet的协议方法

//实现ActionSheet的协议方法

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

//    switch 传入你点击的按钮索引

switch (buttonIndex) {

case 0:

{

//        相册

NSLog(@"相册");

//            判断是否可以调用相册

if ([UIImagePickerController  isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

//                调用相册

[self loadImageWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

}

else{

//            使用Alertview 提示相册不可用

[self creatAlertViewWithMessage:@"相册不可用"];

}

}

break;

case 1:

{

//            相机

NSLog(@"相机");

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

[self loadImageWithSourceType:UIImagePickerControllerSourceTypeCamera];

}

else{

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

}

}

break;

default:

break;

}

}

#pragma mark 实现UIController协议方法

//实现UIController协议方法

//当你选择照片或拍照后选择回传

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

{

NSLog(@"回传的信息%@",info);

if ([info valueForKey:UIImagePickerControllerOriginalImage]) {

//        如果取到了值

UIImage *orgImage=info[UIImagePickerControllerOriginalImage];

UIImage *scaleImage=[ImageTool resizeImagetoSize:CGSizeMake(50, 50) WithImage:orgImage];

//  把图片赋给背景

_background.image=scaleImage;

}

[picker dismissViewControllerAnimated:YES completion:nil];

}

//当你点击取消按钮时候回调(若实现此方法需手动返回上一界面)

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

NSLog(@"取消信息");

[picker dismissViewControllerAnimated:YES completion:nil];

}

#pragma mark 自定义的方法

//封装一个创建AlertView的方法来显示相机 相册可不可用

-(void)creatAlertViewWithMessage:(NSString *)message

{

UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

[alertView show];

}

//封装一个相机相册的方法

-(void)loadImageWithSourceType:(UIImagePickerControllerSourceType)sourceType

{

//    创建了一个pickcontroller对象

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

//    指定picker类型

picker.sourceType=sourceType;

//指定代理对象

picker.delegate=self;

//推出VC  一般都是用模态的方式推出pickerController(本质是个VC)

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

}

//创建按钮触发

-(void)creatButton

{

UIButton  *button=[UIButton buttonWithType:UIButtonTypeCustom];

[button setFrame:CGRectMake((WIDTH-60)/2, (HEIGHT-40)/2, 60, 40)];

[button setTitle:@"功能" forState:UIControlStateNormal];

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

[button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

}

//点击按钮的方法回调

-(void)onClick:(UIButton *)button

{

[_actionSheet showInView:self.view];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

由于我水平一般 以上内容仅供参考如有不适合的地方还请见谅!!!

你可能感兴趣的:(iOS开发相册,照相机的调用)