2010-08-26 81 views
3

我想讓UIImagePickerController在橫向方向代碼中啓動(並保持)。我嘗試瞭解決方案,在這裏(UIImagePickerController in Landscape橫向的UIImagePickerController

//Initialize picker 

UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 


//set Device to Landscape. This will give you a warning. I ignored it. 
//warning: 'UIDevice' may not respond to '-setOrientation:' 
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 

//Set Notifications so that when user rotates phone, the orientation is reset to landscape. 
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

//Refer to the method didRotate: 
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(didRotate:) 
       name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

//Set the picker source as the camera 
picker.sourceType = UIImagePickerControllerSourceTypeCamera; 

//Bring in the picker view 
[self presentModalViewController:picker animated:YES]; 

描述的方法didRotate:

- (void) didRotate:(NSNotification *)notification 
{ 
     //Maintain the camera in Landscape orientation 
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 

} 

但解決方案並不適用於iOS 4.0。相機在iOS 4.0中啓動時,該應用程序不會響應。任何人都可以提出一個解決這個問題嗎?

+0

任何人都請提出一個解決方案。 – random 2010-08-27 13:18:06

+0

看看http://stackoverflow.com/questions/20468335/ios7-ipad-landscape-only-app-using-uiimagepickercontroller/20468336#20468336 – 2013-12-09 10:38:04

回答

1

您可以使用ALAssetsLibrary和資產類別來獲取設備中的圖片,並且可以使用它們以橫向模式和縱向模式顯示,就像uiimagepicker一樣。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [activity startAnimating]; 


    appObj=(ImagePickerAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) 
    { 
     if(result != NULL) 
     { 
      //assets is a mutualable array...for storing the images that are in the device.. 
      [assets addObject:result]; 
     } 
    }; 

    void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) 
    { 
     if(group != nil) 
     { 
      [group enumerateAssetsUsingBlock:assetEnumerator]; 
     } 
     //meth is a user defined method.. 
     [self meth]; 
     [activity stopAnimating]; 
     [activity setHidden:YES]; 
    }; 
    assets = [[NSMutableArray alloc] init]; 
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetGroupEnumerator 
         failureBlock: ^(NSError *error) { NSLog(@"Failure");}]; 
} 


-(void)meth 
{ 
    NSLog(@"%i",[assets count]); 

    if(userOrientation==UIInterfaceOrientationPortrait || userOrientation==UIInterfaceOrientationPortraitUpsideDown) 
    { 
     NSLog(@"haii"); 
     [scrollView removeFromSuperview]; 

     scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)]; 
     scrollView.backgroundColor=[UIColor whiteColor]; 

     NSLog(@"%i",[assets count]); 
     for (int i = 0; i < [assets count]; i++) 
     { 
      imgBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [imgBtn setFrame:CGRectMake((i%4*80)+2,(i/4*80)+2,75,75)]; 
      imgBtn.tag=i; 
      [imgBtn addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside]; 
      ALAsset *asset=[assets objectAtIndex:i]; 
      [imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal]; 
      [scrollView addSubview:imgBtn]; 
     } 
     scrollView.contentSize = CGSizeMake(320,(([assets count]/4)+1)*300); 
    } 

    if(userOrientation==UIInterfaceOrientationLandscapeRight || userOrientation==UIInterfaceOrientationLandscapeLeft) 
    { 
     [scrollView removeFromSuperview]; 
     scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 480,320)]; 
     for (int i = 0; i < [assets count]; i++) 
     { 
      imgBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [imgBtn setFrame:CGRectMake((i%6*80)+2,(i/6*80)+2,75,75)]; 
      imgBtn.tag=i; 
      [imgBtn addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside]; 
      ALAsset *asset=[assets objectAtIndex:i]; 
      [imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal]; 
      [scrollView addSubview:imgBtn]; 
     } 
     scrollView.contentSize = CGSizeMake(480,(([assets count]/4)+1)*300); 
    } 
    [self.view addSubview:scrollView]; 
} 



-(void)imageClicked:(UIButton *)sender 
{ 
    //for picking the images that the user has selected we are using other array "selectedImages" i.e declared in the app delegate 
    ALAsset *asset=[assets objectAtIndex:sender.tag]; 
    [appObj.selectedImages addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]]; 
    NSLog(@"%i",[appObj.selectedImages count]); 
    [self.navigationController popViewControllerAnimated:YES ]; 
} 
0

下面是使用[currentDevice endGeneratingDeviceOrientationNotifications]

UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease]; 
picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
picker.delegate = self; 

// Display the camera. 
[self presentModalViewController:picker animated:YES]; 

// Given by default your orientation is in landscaperight already 
while ([currentDevice isGeneratingDeviceOrientationNotifications]) 
    [currentDevice endGeneratingDeviceOrientationNotifications]; 

來源一個解決辦法:Disable rotation in UIImagePicker