当前众多lbs和sns的项目中都有用户分享图片,上传图片功能,要想实现ipad拍照,需要将UIImagePickerController的sourceType设定为UIImagePickerControllerSourceTypeCamera
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
,之后处理拍照之后
通过UIImageWriteToSavedPhotosAlbum方法将图片保存到图片库
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
.........视图控制器实现
@interface TestBedViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end
@implementation TestBedViewController
//保存完成的回调代理方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
if (!error)
showAlert(@"Image written to photo album");
else
showAlert(@"Error writing to photo album: %@", [error localizedDescription]);
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
SETIMAGE(image);
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
// Provide 2.x compliance
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSDictionary *dict = [NSDictionary dictionaryWithObject:image forKey:@"UIImagePickerControllerOriginalImage"];
[self imagePickerController:picker didFinishPickingMediaWithInfo:dict];
}
// Optional but "expected" dismiss
/*
- (void) imagePickerControllerDidCancel:
(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
*/
- (void) snapImage: (id) sender
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
ipc.allowsImageEditing = NO;
[self presentModalViewController:ipc animated:YES];
}
------------------------------------------------------------------
记住了,核心代码就那3行
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Snap", @selector(snapImage:));
else
showAlert(@"This demo relies on camera access, which is not available on this system. Please run this application on a camera-ready device.");
self.title = @"Image Picker";
}
@end