我正在尝试使用UIImagePickerController拍摄图像。如果属性allowEditing为NO,那么一切正常。但是一旦我将属性值更改为YES,就不会出现图像。它出现在相机和图书馆中。
以下是点按按钮打开相机时执行的代码。
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *controller_imagePicker = [[UIImagePickerController alloc] init];
[controller_imagePicker setDelegate:self];
[controller_imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[controller_imagePicker setAllowsEditing:YES];
controller_imagePicker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:controller_imagePicker animated:YES completion:nil];
}
当我们拍摄图像并对其进行编辑并使用图像时。黑色图像即将出现在相应的图像视图中。
此外,一旦相机打开,我会收到此警告:“快照未呈现的视图会导致空白快照。确保您的视图在屏幕更新后的快照或快照之前至少呈现一次。”
任何想法?
答案 0 :(得分:0)
如果allowEditing,您应该使用UIImagePickerControllerEditedImage
密钥而不是UIImagePickerControllerOriginalImage
。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = nil;
if ([picker allowsEditing]){
image = [info objectForKey:UIImagePickerControllerEditedImage];
} else {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
// do something
}
答案 1 :(得分:0)
#import <MobileCoreServices/UTCoreTypes.h>
….
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePickerCamera =[[UIImagePickerController alloc] init];
imagePickerCamera.delegate = self;
imagePickerCamera.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
imagePickerCamera.allowsEditing = YES;
imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerCamera animated:YES completion:nil];
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePickerAlbum =[[UIImagePickerController alloc] init];
imagePickerAlbum.delegate = self;
imagePickerAlbum.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
imagePickerAlbum.allowsEditing = YES;
imagePickerAlbum.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerAlbum animated:YES completion:nil];
}
#pragma mark - ImagePickerController Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) {
[picker dismissViewControllerAnimated:YES completion:nil];
}
else
{
[popover dismissPopoverAnimated:YES];
}
UIImage* originalImage = nil;
originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerCropRect];
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}