我有一个问题:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {}
只被相机调用,而不是当我试图通过浏览用户的照片来选择图像时。
这是我的 UIImagePicker 委托的初始化代码:
-(void)viewDidLoad{
[self initImagePicker];
}
-(void)initImagePicker{
DLog(@"");
self.imagePickerController.delegate = self;
}
- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
self.imagePickerController.sourceType = sourceType;
if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
// user wants to use the camera interface
//
self.imagePickerController.showsCameraControls = NO;
if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0)
{
// setup our custom overlay view for the camera
//
// ensure that our custom view's frame fits within the parent frame
CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
CGRect newFrame = CGRectMake(0.0,CGRectGetHeight(overlayViewFrame)-15.0,CGRectGetWidth(overlayViewFrame),15.0);
self.view.frame = newFrame;
[self.imagePickerController.cameraOverlayView addSubview:self.view];
[self.view bringSubviewToFront:self.view];
}
}
}
然后在我的根类中,它是 UIImagePicker 类的委托,它是 UIImagePicker 的委托:
#pragma mark -
#pragma mark Toolbar Actions
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// camera is not on this device, don't show the camera button
NSMutableArray *toolbarItems = [NSMutableArray arrayWithCapacity:self.myToolbar.items.count];
[toolbarItems addObjectsFromArray:self.myToolbar.items];
[toolbarItems removeObjectAtIndex:2];
[self.myToolbar setItems:toolbarItems animated:NO];
}
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
self.overlayViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"OverlayViewController"];
self.overlayViewController.delegate = self;
self.overlayViewController.imagePickerController = [[UIImagePickerController alloc] init];
[self.overlayViewController setupImagePicker:sourceType];
[self presentModalViewController:self.overlayViewController.imagePickerController animated:YES];
}
}
我知道这是一个 SO 问题的很多代码,但是对于有实现这个类的经验的人来说,我做错了什么?
对我来说,这条线很奇怪:
self.overlayViewController.imagePickerController = [[UIImagePickerController alloc] init];
我以前从未在所有者之外分配和初始化属性。是这个问题吗?