调用相机,相册实现更换头像
- (void)viewDidLoad{
UIActionSheet *sheet;
//判断是否支持相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册中选择", nil];
}
else{
sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册中选择", nil];
}
[sheet showInView:view];
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch (buttonIndex) {
case0:
return;
case1:
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case2:
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
}
else{
if (buttonIndex == 0) {
return;
}
else
{
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
//跳转到相机或相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES; //可编辑
imagePickerController.sourceType = sourceType;
[EUtility brwView:meBrwView presentModalViewController:imagePickerController animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; //裁减后的图片
NSString * path = [self saveImage:image]; //saveImage
[picker dismissModalViewControllerAnimated:YES];
[EUtility brwView:meBrwView evaluateScript:[NSString stringWithFormat:@"uexPhoto.imagePathCallBack('%@');",path]];
}
//将图片保存到本地
-(NSString*) saveImage:(UIImage*)image
{
BOOL success;
NSFileManager *fileManger = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *imageFilePath = [documentDirectory stringByAppendingPathComponent:@"selfPhpto.jpg"];
// NSLog(@"imageFile ---- %@",imageFilePath);
success = [fileManger fileExistsAtPath:imageFilePath];
if (success) {
success = [fileManger removeItemAtPath:imageFilePath error:&error];
}
//写入文件
[UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFilePath atomically:YES];
UIImage *selfPhoto = [UIImage imageWithContentsOfFile:imageFilePath];
return imageFilePath;
}

浙公网安备 33010602011771号