1.wpc
1.导框架
添加系统框架
MobileCoreServices.framework
头文件
#import <MobileCoreServices/MobileCoreServices.h>
遵守的协议
<UINavigationControllerDelegate,UIImagePickerControllerDelegate
2.方法
#pragma mark - 点击事件
-(void)btnClick:(UIButton *)btn{
//UIImagePickerController:里面封装好了调用相机和相册的逻辑,可直接使用。
UIImagePickerController *pc = [[UIImagePickerController alloc]init];
pc.delegate = self;
if (btn.tag == 100) {
//拍照
//设置资源类型 --> 先要检测要设置的资源类型是否可用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[pc setSourceType:UIImagePickerControllerSourceTypeCamera];
//根据开发习惯,UIImagePickerController以present的方式出现
[self presentViewController:pc animated:YES completion:nil];
[pc release];
}
else{
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示" message:@"相机不可用" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[av show];
}
}
else{
//相册选择
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
[pc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
//设置允许编辑
pc.allowsEditing = YES;
[self presentViewController:pc animated:YES completion:nil];
[pc release];
}
else{
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无法访问相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[av show];
}
}
}
#pragma mark - UIImagePickerControllerDelegate相关
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//选择完成 --> 判断选择完的资源是image还是media
NSString *str = [info objectForKey:UIImagePickerControllerMediaType];
if ([str isEqualToString:(NSString *)kUTTypeImage]) {
//如果取到的资源是image --> 把image显示在_photoIv上
//UIImagePickerControllerEditedImage:编辑后的照片
//UIImagePickerControllerOriginalImage:原图
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
_photoIv.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"cancel");
}
2.xl
1.导框架
添加系统框架
MobileCoreServices.framework
遵守的协议
UINavigationControllerDelegate,UIActionSheetDelegate
2.方法
//actionSheet协议中的方法,点击跳转到相册
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==2) {
//获取相册
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];//相册中获取
picker.delegate=self;
//跳转到相册界面
[self presentViewController:picker animated:YES completion:nil];
}
}