使用UIImagePickerController打开图片库和相机选择图片修改头像的主要方法如下,
声明:这个是iphone版本的,ipad版本的使用这个不行,因为iPad要用UIPopover才可以。
- (void)viewDidLoad
{
[super viewDidLoad];
//获取Documents文件夹目录
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath= [path objectAtIndex:0];
//指定新建文件夹路径
NSString *imageDocPath= [documentPath stringByAppendingPathComponent:@"ImageFile"];
//创建ImageFile文件夹
[[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil];
//保存图片的路径
self.imagePath= [imageDocPath stringByAppendingPathComponent:@"image.png"];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
//根据图片路径载入图片
UIImage *image=[UIImage imageWithContentsOfFile:self.imagePath];
if (image== nil) {
//显示默认
[changeImg setBackgroundImage:[UIImage imageNamed:@"user_photo@2x.png"] forState:UIControlStateNormal];
}else {
//显示保存过的
[changeImg setBackgroundImage:image forState:UIControlStateNormal];
}
}
- (void)dealloc {
[imagePath release];
[changeImg release];
[super dealloc];
}
- (IBAction)changeImage:(id)sender {
UIActionSheet *myActionSheet= [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles: @"从相册选择", @"拍照",nil];
[myActionSheet showInView:self.view];
[myActionSheet release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
//从相册选择
[self LocalPhoto];
break;
case 1:
//拍照
[self takePhoto];
break;
default:
break;
}
}
//从相册选择
-(void)LocalPhoto{
UIImagePickerController *picker= [[UIImagePickerController alloc] init];
//资源类型为图片库
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate=self;
//设置选择后的图片可被编辑
picker.allowsEditing=YES;
[self presentModalViewController:picker animated:YES];
[picker release];
}
//拍照
-(void)takePhoto{
//资源类型为照相机
UIImagePickerControllerSourceTypesourceType=UIImagePickerControllerSourceTypeCamera;
//判断是否有相机
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *picker= [[UIImagePickerController alloc] init];
picker.delegate=self;
//设置拍照后的图片可被编辑
picker.allowsEditing=YES;
//资源类型为照相机
picker.sourceType=sourceType;
[self presentModalViewController:picker animated:YES];
[picker release];
}else {
NSLog(@"该设备无摄像头");
}
}
#pragma Delegate method UIImagePickerControllerDelegate
//图像选取器的委托方法,选完图片后回调该方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
//当图片不为空时显示图片并保存图片
if (image != nil) {
//图片显示在界面上
[changeImg setBackgroundImage:image forState:UIControlStateNormal];
//以下是保存文件到沙盒路径下
//把图片转成NSData类型的数据来保存文件
NSData *data;
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
//返回为png图像。
data=UIImagePNGRepresentation(image);
}else {
//返回为JPEG图像。
data=UIImageJPEGRepresentation(image, 1.0);
}
//保存
[[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];
}
//关闭相册界面
[picker dismissModalViewControllerAnimated:YES];
}