UIImagePickerController:切换源类型不起作用
Posted
技术标签:
【中文标题】UIImagePickerController:切换源类型不起作用【英文标题】:UIImagePickerController: switching source types does not work 【发布时间】:2018-04-07 04:35:36 【问题描述】:我有一个带有自定义叠加视图的 UIImagePickerController。视图控制器最初以相机作为源类型打开。但是有一个按钮可以将源类型切换为相册。如果他们进入相册模式,他们可以点击cancel
将源类型切换回相机。
这适用于第一轮。但是如果他们第二次按下相册按钮(在已经进入相册模式并取消它之后),屏幕会加载白色背景但不会加载用户的照片库。然后该应用程序被卡住。
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"CameraView" owner:self options:nil];
self.overlayView.frame = ipc.cameraOverlayView.frame;
ipc.cameraOverlayView = self.overlayView;
self.overlayView = nil;
CGSize screenBounds = [UIScreen mainScreen].bounds.size;
int cameraViewHeight;
int adjustedYPosition;
/*Center Uiimagepickercontroller in screen */
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
cameraViewHeight = screenBounds.width * 1.333;
adjustedYPosition = (screenBounds.height - cameraViewHeight) / 2;
NSLog(@"portrait screenbounds width: %f, screenbounds height: %f", screenBounds.width, screenBounds.height);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
ipc.cameraViewTransform = translate;
NSLog(@"in portrait value is %d", adjustedYPosition);
else
//landscape mode
cameraViewHeight = screenBounds.height * 1.333;
adjustedYPosition = (screenBounds.width - cameraViewHeight) / 2;
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
ipc.cameraViewTransform = translate;
ipc.showsCameraControls = NO;
ipc.tabBarController.tabBar.hidden = YES;
else
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:ipc animated:YES completion:nil];
[_activityIndicator stopAnimating];
- (IBAction)albumView:(id)sender
[ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
/*navigate to previous tab */
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
[picker dismissViewControllerAnimated:YES completion:nil];
LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController;
[self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];
else
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
【问题讨论】:
【参考方案1】:我不确定为什么会这样,所以如果有人可以解释,我会将他们的回答标记为正确。但这是我的解决方案:
- (IBAction)albumView:(id)sender
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker.view setFrame:self.view.frame];
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagePicker setDelegate:self];
[ipc presentViewController:imagePicker animated:YES completion:nil];
- (IBAction)cancel_IPC:(id)sender
[self imagePickerControllerDidCancel:ipc];
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[picker dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController;
[self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage* origImage = [info objectForKey:UIImagePickerControllerOriginalImage];
/*do whatever you need to do with the img*/
[picker dismissViewControllerAnimated:YES completion:nil];
/*dismiss the original UIImagePickerController in background */
if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
[ipc dismissViewControllerAnimated:YES completion:nil];
[mLocationManager stopUpdatingLocation];
【讨论】:
以上是关于UIImagePickerController:切换源类型不起作用的主要内容,如果未能解决你的问题,请参考以下文章
UIImagePickerController、自定义 UIButton 和 AutoLayout
OCMock 模拟 UIImagePickerController