在iPhone应用程序中,我想从Camera& amp;通过编程方式保存到照片库。在iPhone模拟器中可以吗? 所以请告诉我你的任何链接或材料。
提前致谢
答案 0 :(得分:18)
您可以使用uiimagepickercontroller来实现此目的。首先,导入MobileCoreServices.framework。然后拿起相机:
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];
}
然后,通过实现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];
}
有关图片的更多信息:didFinishSaving ....可以在这里找到
另请看这篇文章
https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more
答案 1 :(得分:5)
在模拟器中无法实现。您必须使用该设备。
答案 2 :(得分:2)
我希望你意识到模拟器没有摄像头.....所以我想那是不可能的。您绝对可以使用设备....您可以使用UIImagePickerController来捕获图像。
答案 3 :(得分:0)
以编程方式拍摄照片需要3个先决条件:
设备需要通过
进行相机检查 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
需要隐藏相机控件(选择器是UIImagePickerController)
picker.showsCameraControls = NO;
使用UIImagePickerController中的takePicture
[picker takePicture];
旁注,因为它确实让我误解了几分钟,takePicture方法也驳回了这个观点。
此处还发布了更完整的答案Other Stack answer