使用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"];
//創(chuàng)建ImageFile文件夾
[[NSFileManager?defaultManager]?createDirectoryAtPath:imageDocPath?withIntermediateDirectories:YES?attributes:nil?error:nil];
//保存圖片的路徑
self.imagePath=?[imageDocPath?stringByAppendingPathComponent:@"image.png"];
}
-(void)viewWillAppear:(BOOL)animated{
[super?viewWillAppear:YES];
//根據(jù)圖片路徑載入圖片
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;
//設(shè)置選擇后的圖片可被編輯
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;
//設(shè)置拍照后的圖片可被編輯
picker.allowsEditing=YES;
//資源類型為照相機
picker.sourceType=sourceType;
[self?presentModalViewController:picker?animated:YES];
[picker?release];
}else?{
NSLog(@"該設(shè)備無攝像頭");
}
}
#pragma?Delegate?method?UIImagePickerControllerDelegate
//圖像選取器的委托方法仍侥,選完圖片后回調(diào)該方法
-(void)imagePickerController:(UIImagePickerController?*)picker?didFinishPickingImage:(UIImage?*)image?editingInfo:(NSDictionary?*)editingInfo{
//當圖片不為空時顯示圖片并保存圖片
if?(image?!=?nil)?{
//圖片顯示在界面上
[changeImg?setBackgroundImage:image?forState:UIControlStateNormal];
//以下是保存文件到沙盒路徑下
//把圖片轉(zhuǎn)成NSData類型的數(shù)據(jù)來保存文件
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];
}
//關(guān)閉相冊界面
[picker?dismissModalViewControllerAnimated:YES];
}