将我的项目限制为纵向仅将照片视图保留为横向/纵向

时间:2013-02-01 14:06:01

标签: ios xcode facebook navigation photo-gallery

我希望我的整个项目只能以纵向模式运行,只有用于查看照片的视图可以打开风景,就像使用Facebook一样(我们查看照片,他们转向风景,但其他视图仍然是纵向)我想要要知道它是如何完成的,因为我有一个表格视图,我从服务器从数据库获取的图像,每个特定的数据包含不同数量的照片,所以现在我希望在用户选择照片后,他们应该完全打开可以在风景和肖像中观看,我也尝试过

我正在使用ios6

- (BOOL)shouldAutorotate
{
    return NO;
}

在所有视图中实现,以便他们可以保持纵向但仍然转向横向如何限制它,请任何人告诉我,在我的一个班级中我有表格视图中的图像(从数据库加载) )我还有另一个课程,可以打开表格中选定的照片

我的PhotoView.m课程

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSDictionary *dic = [photos objectAtIndex:indexPath.row];
    NSLog(@" *** url is %@",[dic objectForKey:@"url"]);
    [self.delegate showPhotoAtUrl:[dic objectForKey:@"url"]];
 }

我的PhotoViewController.h类

   #import <UIKit/UIKit.h>

@interface PhotoViewController : UIViewController <UIScrollViewDelegate>
{
    NSString *url;
    UIButton *doneButton;
}

@property (nonatomic, retain) UIImageView *imageView;

- (id) initWithPhotoUrl:(NSString *)photoUrl;

@end

和PhotoViewController.m类

#import "PhotoViewController.h"

@interface PhotoViewController ()

@end

@implementation PhotoViewController

@synthesize imageView;

- (id) initWithPhotoUrl:(NSString *)photoUrl
{
    self = [super init];
    if(self) {
        url = [photoUrl retain];
    }
    return self;
}

- (void)dealloc
{
    [url release];
    self.imageView = nil;
    [doneButton release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.tag = 1;
    spinner.center = self.view.center;
    [spinner startAnimating];
    [self.view addSubview:spinner];
    [spinner release];
    [self performSelectorInBackground:@selector(loadPhotoData) withObject:nil];

    doneButton  = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [doneButton addTarget:self action:@selector(doneTouched) forControlEvents:UIControlEventTouchUpInside];
    [doneButton setTitle:@"done" forState:UIControlStateNormal];
    [doneButton setBackgroundColor:[UIColor clearColor]];    
    doneButton.frame = CGRectMake(2, 2, 60, 30);
    [self.view addSubview:doneButton];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = @"";
    self.navigationController.navigationBarHidden = YES;
}

- (void) doneTouched
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) loadComplete:(NSData *)imageData
{
    if(!imageData) return;

    UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)[self.view viewWithTag:1];
    if(spinner) {
        [spinner stopAnimating];
        [spinner removeFromSuperview];
    }

    UIImage *img = [UIImage imageWithData:imageData];

    float scale = MIN(self.view.frame.size.width/img.size.width, self.view.frame.size.height/img.size.height);
    self.imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
    self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width*scale, self.imageView.image.size.height*scale);

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.imageView.center = scrollView.center;
    scrollView.delegate = self;
    scrollView.contentSize = self.imageView.frame.size;
    scrollView.minimumZoomScale = 1;
    scrollView.maximumZoomScale = 2;
    [scrollView addSubview:self.imageView];
    [self.view addSubview:scrollView];
    [scrollView release];

    [self.view bringSubviewToFront:doneButton];
}

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void) loadPhotoData
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    [self performSelectorOnMainThread:@selector(loadComplete:) withObject:imageData waitUntilDone:NO];
    [pool drain];
}

@end

并且在主类中我有这个方法调用PhotoViewController来加载所选的照片

- (void) showPhotoAtUrl:(NSString *)url
{

    PhotoViewController *vc = [[PhotoViewController alloc] initWithPhotoUrl:url];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

现在我的问题是如何在从桌面中选择要在FB应用程序中打开的相同类型的视图中打开后如何获取图像(在纵向/横向兼容视图中打开所选照片)我是只能在我的PhotoViewController类中获得一张照片可以任何人告诉我如何将所有照片添加到PhotoViewController类,以便我得到我想要的效果?...任何代码帮助将非常有帮助..谢谢提前贡献你的时间

简而言之,我有两件事要做:

  1. 我必须将我的项目限制为仅限肖像模式,只留下照片的视图以兼容横向/纵向。

  2. 我目前正在我的桌面视图中查看照片,我希望在查看照片时,选择以与FB或其他应用程序相同的样式(横向/纵向兼容)打开。

    < / LI>

1 个答案:

答案 0 :(得分:0)

These only work on iOS 6.0

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}
    The method will return which orientation mode to use first.

    Below are supportedInterfaceOrientations:

    UIInterfaceOrientationMaskPortrait
    UIInterfaceOrientationMaskLandscapeLeft
    UIInterfaceOrientationMaskLandscapeRight
    UIInterfaceOrientationMaskPortraitUpsideDown 
    UIInterfaceOrientationMaskLandscape 
    UIInterfaceOrientationMaskAll
    UIInterfaceOrientationMaskAllButUpsideDown

    For iOS > 4.0
        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }


Other ViewControllers will have 
-(BOOL)shouldAutorotate { return NO; } 
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation{     
return NO;
}