快到新手了。我的应用程序处于iPad的横向模式,我想在横向模式下以UIImageView上传图像。当我尝试上传图像时,应用程序更改为纵向模式并崩溃。在Objective-c中,我看到了覆盖UIImagePickerController和UIViewController
的类别对于UIImagePickerController
#import "UIImagePickerController+OrientationFix.h"
@implementation UIImagePickerController (OrientationFix)
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
@end
对于UIViewController
#import "UIViewController+OrientationFix.h"
@implementation UIViewController (OrientationFix)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
@end
但是在swift中我无法做到这一点,它显示错误,因为应该没有AutorotateToInterfaceOrientation。如果有人可以帮助我。
我的代码
@IBAction func PhotoClicked(sender: AnyObject) {
if( controllerAvailable()){
let imageController = UIImagePickerController()
imageController.editing = false
imageController.delegate = self
let alert = UIAlertController(title: "get a picture from", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let libButton = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default) { (alert) -> Void in
imageController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(imageController, animated: true, completion: nil)
}
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
let cameraButton = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) { (alert) -> Void in
println("Take Photo")
imageController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(imageController, animated: true, completion: nil)
}
alert.addAction(cameraButton)
} else {
println("Camera not available")
}
let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alert) -> Void in
println("Cancel Pressed")
}
alert.addAction(libButton)
alert.addAction(cancelButton)
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = sender as UIView
popoverController.sourceRect = sender.bounds
}
self.presentViewController(alert, animated: true, completion: nil)
}
else {
var actionSheet:UIActionSheet
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
actionSheet = UIActionSheet(title: "Select a picture", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil,otherButtonTitles:"Select photo from library", "Take a picture")
} else {
actionSheet = UIActionSheet(title: "select a picture", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil,otherButtonTitles:"Select photo from library")
}
actionSheet.delegate = self
actionSheet.showInView(self.view)
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, nil)
imgProfile.image = image;
}
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
println("Title : \(actionSheet.buttonTitleAtIndex(buttonIndex))")
println("Button Index : \(buttonIndex)")
let imageController = UIImagePickerController()
imageController.editing = false
imageController.delegate = self
if( buttonIndex == 1){
imageController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
} else if(buttonIndex == 2){
imageController.sourceType = UIImagePickerControllerSourceType.Camera
} else {
}
self.presentViewController(imageController, animated: true, completion: nil)
}
func controllerAvailable() -> Bool {
if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {
return true;
}
else {
return false;
}
}
崩溃讯息
2015-07-17 00:09:31.007 Stenteq[777:17782] *** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [PUUIAlbumListViewController shouldAutorotate] is returning YES'
libc++abi.dylib: terminating with uncaught exception of type NSException