Search code examples
iosxcodefacebooknavigationphoto-gallery

Restrict my project to portrait only leaving photo view's in landscape/portrait


I want my whole project to run in portrait mode only and only the view that's for viewing the photo's can turn on landscape same as if using the facebook (we view photo's they turn landscape also but other view's remains in portrait) i want to know that how it's done as i have a table view that has images that i get from the DB from the server and every particular data contains different number's of photo's so now i want that after the user select's the photo's they should open up fully and can be viewed in landscape and in portrait as well i did tried

I am working with ios6

- (BOOL)shouldAutorotate
{
    return NO;
}

implementing in all the view's so that they can remain in portrait but still they turn in landscape how should i restrict it and please can any one tell me that in one of my class i have images in table view(loaded from the DB) and i have another class that open's up the selected photo from the table

My PhotoView.m class

- (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"]];
 }

My PhotoViewController.h class

   #import <UIKit/UIKit.h>

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

@property (nonatomic, retain) UIImageView *imageView;

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

@end

and PhotoViewController.m class

#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

and in the main class i have have this method that calls the PhotoViewController to load the selected photo

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

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

Now my problem is how should i get the images after getting selected from the table to be open up in the same type of the view that open's up in FB app (open's the selected photo in portrait/landscape compatible view) i am only able to get one photo in my PhotoViewController class can any one tell me how can i add all the photo's to the PhotoViewController class so that i get the effect that i want ?... Any code help will be very helpful .. Thanks in Advance for contributing your time

In Short i have two things to do :

  1. I have to restrict my project to only portrait mode leaving only the photo's view to have landscape/portrait compatibility.

  2. I am currently having the photo's in my table view that i want on selection to open up in the same style(landscape/portrait compatibile) as FB or other app's do while viewing the photo's.


Solution

  • 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;
    }