[IOS]用UIImagePickerController调用系统照相机
原创
©著作权归作者所有:来自51CTO博客作者mb63e0703549da8的原创作品,请联系作者获取转载授权,否则将追究法律责任
iPhone API 中提供了调用系统照相机的接口,我们只需调用相应的界面,即刻在自己的程序中获取相机图片。下面是一个非常简单的调用系统照相机的例子,相应的界面如下:
[img]http://dl.iteye.com/upload/attachment/0072/0549/25dab3cf-5791-3a4f-9000-60d1a9ceaf25.jpg[/img]
最后可以编辑图片和使用相应的图片。
下面是主要代码:
- (void) addPicEvent
{
//先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
//sourceType = UIImagePickerControllerSourceTypeCamera; //照相机
//sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //相片库
//sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)saveImage:(UIImage *)image {
NSLog(@"保存");
}
#pragma mark –
#pragma mark Camera View Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [[info objectForKey:UIImagePickerControllerEditedImage] retain];
[self performSelector:@selector(saveImage:)
withObject:image
afterDelay:0.5];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}