相冊丙猬、相機(jī)UIImagePickerController? 相冊、相機(jī)的調(diào)用都是用此類费韭,sourceType不一致協(xié)議UINavigationControllerDelegateUIImagePickerControllerDelegate當(dāng)你選擇照片或拍照后回傳圖片信息imagePickerController:didFinishPickingMediaWithInfo:當(dāng)你點(diǎn)擊取消鍵時回調(diào)(若實(shí)現(xiàn)此方法茧球,需手動返回上一界面)imagePickerControllerDidCancel:? ? ??
? 例子:將從系統(tǒng)相框選出照片作為屏幕背景建一個工具類.m中#import#import@interface ImageTool : NSObject//實(shí)現(xiàn)一個方法壓縮圖片+(UIImage *)resizeImagetoSize:(CGSize)size WithImage:(UIImage*)image;@end工具類.h中#import "ImageTool.h"@implementation ImageTool//實(shí)現(xiàn)一個類方法壓 縮圖片+(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();? ? //? ? 結(jié)束繪圖? ? UIGraphicsEndImageContext();? ? ? ? ? ? return resultImage;}@endVC類.h中#import "ViewController.h"#import "ImageTool.h"http://定義屏幕寬高的宏#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];
}
//創(chuàng)建圖片背景
-(void)creatBackground
{
_background =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
_background.backgroundColor=[UIColor yellowColor];
[self.view addSubview:_background];
}
//創(chuàng)建
-(void)creatActionSheet
{
_actionSheet=[[UIActionSheet alloc]initWithTitle:@"請選擇功能" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相冊",@"相機(jī)", nil];
}
#pragma mark 實(shí)現(xiàn)ActionSheet的協(xié)議方法
//實(shí)現(xiàn)ActionSheet的協(xié)議方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//? ? switch 傳入你點(diǎn)擊的按鈕索引
switch (buttonIndex) {
case 0:
{
//? ? ? ? 相冊
NSLog(@"相冊");
//? ? ? ? ? ? 判斷是否可以調(diào)用相冊
if ([UIImagePickerController? isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//? ? ? ? ? ? ? ? 調(diào)用相冊
[self loadImageWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
else{
//? ? ? ? ? ? 使用Alertview 提示相冊不可用
[self creatAlertViewWithMessage:@"相冊不可用"];
}
}
break;
case 1:
{
//? ? ? ? ? ? 相機(jī)
NSLog(@"相機(jī)");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self loadImageWithSourceType:UIImagePickerControllerSourceTypeCamera];
}
else{
[self creatAlertViewWithMessage:@"相機(jī)不可用"];
}
}
break;
default:
break;
}
}
#pragma mark 實(shí)現(xiàn)UIController協(xié)議方法
//實(shí)現(xiàn)UIController協(xié)議方法
//當(dāng)你選擇照片或拍照后選擇回傳
-(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];
}
//當(dāng)你點(diǎn)擊取消按鈕時候回調(diào)(若實(shí)現(xiàn)此方法需手動返回上一界面)
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"取消信息");
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark 自定義的方法
//封裝一個創(chuàng)建AlertView的方法來顯示相機(jī) 相冊可不可用
-(void)creatAlertViewWithMessage:(NSString *)message
{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alertView show];
}
//封裝一個相機(jī)相冊的方法
-(void)loadImageWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
//? ? 創(chuàng)建了一個pickcontroller對象
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
//? ? 指定picker類型
picker.sourceType=sourceType;
//指定代理對象
picker.delegate=self;
//推出VC? 一般都是用模態(tài)的方式推出pickerController(本質(zhì)是個VC)
[self presentViewController:picker animated:YES completion:nil];
}
//創(chuàng)建按鈕觸發(fā)
-(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];
}
//點(diǎn)擊按鈕的方法回調(diào)
-(void)onClick:(UIButton *)button
{
[_actionSheet showInView:self.view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
由于我水平一般 以上內(nèi)容僅供參考如有不適合的地方還請見諒!!!