UIImagePicker 控制器,在点击相机按钮的时候,预览和使用按钮显示,但是图片没有保存在图片相册中。
代码:
-(void)viewDidAppear:(BOOL)animated{
picker = [[UIImagePickerController alloc] init];
// Set the image picker source:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Hide the controls:
picker.showsCameraControls = YES;
picker.navigationBarHidden = YES;
// Make camera view full screen:
picker.wantsFullScreenLayout = YES;
// Insert the overlay:
picker.cameraOverlayView = self.view;
self.view.backgroundColor=[UIColor clearColor];
// Show the picker:
[self presentModalViewController:picker animated:YES];
}
这样设置:
首先设置UIImagePickerController
代理本身。
-(void)viewDidAppear:(BOOL)animated{
picker = [[UIImagePickerController alloc] init];
picker.delegte = self ;
// Set the image picker source:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Hide the controls:
picker.showsCameraControls = YES;
picker.navigationBarHidden = YES;
// Make camera view full screen:
picker.wantsFullScreenLayout = YES;
// Insert the overlay:
picker.cameraOverlayView = self.view;
self.view.backgroundColor=[UIColor clearColor];
// Show the picker:
[self presentModalViewController:picker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImageWriteToSavedPhotosAlbum(image,self,
@selector(image:finishedSavingWithError:contextInfo:),
nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
if (error)
alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
else
alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Image saved to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
你贴出来的代码仅是调用Camera.并没有编写在“Use”这张图片后所执行的回调。
你还需要:
1.在弹出Camera的控制器的头文件.h中添加两个代理协议
@interface youViewController:UIViewController
<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
2.设置UIImagePickerController的代理为当前控制器
picker.delegte = self ;
只有加上了这一句,回调函数才会被执行
3.在当前控制器中实现 UIImagePickerController的协议方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImageWriteToSavedPhotosAlbum(image,self,
@selector(image:finishedSavingWithError:contextInfo:),
nil); //保存图片到Album
}