[英]How to take picture from Camera & saved in Photo Gallery by programmatically?
In iPhone Apps, I want to take pictures from Camera & save into Photo Gallery by programmatically. 在iPhone应用程序中,我想从相机拍摄照片并通过编程方式保存到照片库。 Is it possible in iPhone Simulator? 在iPhone模拟器中可以吗? so please tell me any link or materials you have. 所以请告诉我你的任何链接或材料。
Thanks in advance 提前致谢
You would use uiimagepickercontroller for this purpose. 您可以使用uiimagepickercontroller来实现此目的。 First of all, import the MobileCoreServices.framework. 首先,导入MobileCoreServices.framework。 Then pull up the camera: 然后拉起相机:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsupported!"
message:@"Camera does not support photo capturing."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unavailable!"
message:@"This device does not have a camera."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Then, save the photo by implementing the uiimagepickercontrollerdelegate and a save function: 然后,通过实现uiimagepickercontrollerdelegate和保存功能来保存照片:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"Media Info: %@", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
UIAlertView *alert;
//NSLog(@"Image:%@", image);
if (error) {
alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
More info on image:didFinishSaving.... can be found here 有关图像的更多信息:didFinishSaving ....可以在这里找到
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html
Also have a look at this post 另外看看这篇文章
https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more
在模拟器中是不可能的。你必须使用该设备。
I do hope you realize that the simulator does not have a camera..... So I guess thats not possible. 我希望你意识到模拟器没有相机.....所以我猜这不可能。 You can definitely use a device.... You can make use of UIImagePickerController for capturing images. 你绝对可以使用设备......你可以利用UIImagePickerController来捕获图像。
There are 3 prerequesites needed to take a picture programmatically: 以编程方式拍摄照片需要3个先决条件:
The device needs to have a camera check by 设备需要通过相机检查
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
Camera controls need to be hidden (picker is a UIImagePickerController) 需要隐藏相机控件(选择器是UIImagePickerController)
picker.showsCameraControls = NO;
use the takePicture from UIImagePickerController 使用UIImagePickerController中的takePicture
[picker takePicture]; [picker takePicture];
Side note because it did bug me for a few minutes, the takePicture method also dismisses the view. 旁注,因为它确实让我误解了几分钟,takePicture方法也驳回了视图。
Also posted a more complete answer here Other Stack answer 另外在这里贴出了更完整的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.