当我用iPad拍照时,控制台上会显示以下消息:
快照未呈现的视图会导致空快照。确保在屏幕更新后快照或快照之前,您的视图至少呈现过一次。
这是我拍摄照片并保存的代码:
- (IBAction)selectPhoto:(id)sender {
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* camera = [UIAlertAction actionWithTitle:@"Take Photo" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.allowsEditing = YES;
_imagePickerController.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[_imagePickerController setSourceType: UIImagePickerControllerSourceTypeCamera];
_imagePickerController.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
else {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Camera not detected"
message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
}];
[alertController addAction:camera];
[alertController.view layoutIfNeeded];
UIPopoverPresentationController *pop = alertController.popoverPresentationController;
alertController.popoverPresentationController.sourceRect = self.button.frame;
alertController.popoverPresentationController.sourceView = self.view;
pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
pop.delegate = self;
[self presentViewController:alertController animated:YES completion:nil];
}
获取新图片:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
_img = [info objectForKey:UIImagePickerControllerEditedImage];
[_button setBackgroundImage:_img forState:UIControlStateNormal];
[_button setTitle:@"" forState:UIControlStateNormal];
}
[self.imagePickerController dismissViewControllerAnimated:YES completion:nil];
}
单击拍照时会出现警告。
答案 0 :(得分:2)
希望这会有所帮助:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *yourImage = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}