最近處理了一個(gè)關(guān)于默認(rèn)橫屏調(diào)用系統(tǒng)相冊(cè)/相機(jī)莫名閃退的bug,排除了權(quán)限配置問(wèn)題之后 ,很大的原因是因?yàn)闄M豎屏切換的問(wèn)題,有時(shí)候不閃退,但是相機(jī)只出現(xiàn)一半的情況,很是詭異,廢話(huà)不多說(shuō),直接上代碼
1.相機(jī)
在判斷完APP相機(jī)權(quán)限為開(kāi)啟之后,調(diào)用系統(tǒng)打開(kāi)相機(jī) 方法 ,在此之前發(fā)送一個(gè)通知(目的在后面)
?NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
? ? ? ? ? ? [dic setObject:@"1" forKey:@"deviceOrientation"];//允許豎屏
? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeDevicesOrientation" object:nil userInfo:dic];
? ? ? ? ? ? UIImagePickerController * picker = [[UIImagePickerController alloc] init];
? ? ? ? ? ? picker.sourceType = UIImagePickerControllerSourceTypeCamera;
? ? ? ? ? ? picker.delegate= weakSelf;
? ? ? ? ? ? picker.allowsEditing=NO;
? ? ? ? ? ? [weakSelfpresentViewController:picker animated:YES completion:nil];
2.在appdelegate 的 didFinishLaunchingWithOptions 方法?里面創(chuàng)建一個(gè)監(jiān)聽(tīng)者 并 實(shí)現(xiàn)監(jiān)聽(tīng)通知的方法
@property(nonatomic,assign)NSUInteger ?currentOrientation;
//創(chuàng)建一個(gè)接收橫豎轉(zhuǎn)向的通知
? ? [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeDevicesOrientation:) name:@"ChangeDevicesOrientation" object:nil];
-(void)ChangeDevicesOrientation:(NSNotification*)noInfo
{
? ? NSSLog(@"輸出一看傳遞過(guò)來(lái)的信息:%@",noInfo.userInfo);
? ? //UIImagePickerController
? ? if ([[noInfo.userInfo objectForKey:@"deviceOrientation"] intValue]==1)
? ? {
? ? ? ? self.currentOrientation=UIInterfaceOrientationMaskPortrait; //豎屏
? ? }
? ? else
? ? {
? ? ? ? self.currentOrientation=UIInterfaceOrientationMaskLandscapeRight;//home鍵在右側(cè)的橫屏
? ? }
}
//supportedInterfaceOrientationsForWindow這個(gè)方法就是改變當(dāng)前界面橫豎狀態(tài)的方法,這個(gè)方法在每個(gè)界面加載的時(shí)候都會(huì)調(diào)用一次可以在方法里加入輸出查看當(dāng)前界面的類(lèi)型.
- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
? ? return self.currentOrientation;
}
3.同樣需要在相冊(cè)的點(diǎn)擊方法里面也需要發(fā)送通知,
NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
? ? ? ? ? ? [dicsetObject:@"1" forKey:@"deviceOrientation"];//允許豎屏
? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeDevicesOrientation" object:nil userInfo:dic];
? ? ? ? ? ? UIImagePickerController * picker = [[UIImagePickerController alloc] init];
? ? ? ? ? ? picker.sourceType = UIImagePickerControllerCameraCaptureModePhoto;
? ? ? ? ? ? picker.delegate=self;
? ? ? ? ? ? picker.allowsEditing=NO;
? ? ? ? ? ? [self presentViewController:picker animated:YES completion:nil];
4.在點(diǎn)擊了相機(jī)/相冊(cè)的取消按鈕之后需要把狀態(tài)改變回來(lái),同樣是發(fā)送通知改變
-(void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
? ? NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
? ? [dicsetObject:@"0" forKey:@"deviceOrientation"];//不允許豎屏
? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeDevicesOrientation" object:nil userInfo:dic];
? ? [pickerdismissViewControllerAnimated:YES completion:nil];
}
5.在相機(jī)/相冊(cè)的代理方法里面也需要發(fā)送通知改變?yōu)樵瓉?lái)的橫豎狀態(tài)
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
? ? NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
? ? [dicsetObject:@"0" forKey:@"deviceOrientation"];//不允許豎屏
? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeDevicesOrientation" object:nil userInfo:dic];
........
}