3

I want to open iphone saved images in my application. my application working in landscape mode. when I trying to load all saved photo from iphone library using presentModalViewController method, it will open in portrait mode. I want that in landscape mode. here is the code:

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

picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.allowsEditing = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
[picker setNavigationBarHidden:TRUE];

[self presentModalViewController:picker animated:NO];
NSLog(@"%@", self.view.subviews);
[picker release];  

can any one help me.. Thanks in advance.

Tony
  • 4,311
  • 26
  • 49
Ronak
  • 165
  • 1
  • 3
  • 15

6 Answers6

4

You can't. Quoth the documentation:

The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing.

It's a bit more work, but you can use the AssetsLibrary framework to access the list of images and create your own image picker.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • 1
    This shows how to create an image picker with Assets Library: http://www.icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/ – Caroline Apr 20 '11 at 23:52
  • but I need your another help, – Ronak Apr 21 '11 at 00:32
  • can any one say that How can I reference ALAssetsLibrary to my project? – Ronak Apr 21 '11 at 00:33
  • I am getting following error msg: "_OBJC_CLASS_$_ALAssetsLibrary", referenced from: objc-class-ref-to-ALAssetsLibrary in uploadfromGallery.o ld: symbol(s) not found collect2: ld returned 1 exit status - please Help me out – Ronak Apr 21 '11 at 00:39
  • @Ronak: You have to add the AssetsLibrary framework to your project, which you do [in the same way you add any other existing framework](http://stackoverflow.com/questions/3352664). – Anomie Apr 21 '11 at 02:29
  • Thanks for help me out of this issue. but I figured out with simple following code: [self.view addSubview:imgPicker.view]; [imgPicker viewWillAppear:YES]; [imgPicker viewDidAppear:YES]; instead of [self presentModalViewController:picker animated:NO]; – Ronak Apr 21 '11 at 18:24
  • @Ronak: Note that that method of displaying the picker may not be supported. The documentation only refers to `presentModalViewController:animated:` or `UIPopoverController` as methods for presenting it. – Anomie Apr 21 '11 at 18:36
  • @Ronak: it works on simulator, but doesn't actually work on iOS4 devices. Tried that :) – gilm Jan 31 '12 at 15:17
2

I have tried the code with some additions

[self.navigationController setNavigationBarHidden:YES];
[self addChildViewController:picker];
[self.view addSubview:picker.view];
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
picker.view.frame = self.view.bounds;
[picker didMoveToParentViewController:self];


(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   self.imageView.image = chosenImage;
   [picker.view removeFromSuperview];
   [picker removeFromParentViewController];
}


(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{
   [picker.view removeFromSuperview];
   [picker removeFromParentViewController];
}
Vaibhav Sharma
  • 1,123
  • 10
  • 22
2

we cant get UIImagePickerController in landscape...

but we can get the images in our device into an array and display them in landscape mode and in portrait mode....itseems same like UIImagePickerController...

we should use ALAsset class and ALAssetsLibrary for this..

void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
    if(result != NULL) 
    {
        [assets addObject:result];
    }
};

void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) 
{
    if(group != nil)
    {
        [group enumerateAssetsUsingBlock:assetEnumerator];
    }
    [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");}];

in viewDidLoad

-(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];
 }
Hiren
  • 12,720
  • 7
  • 52
  • 72
phani yelugula
  • 278
  • 3
  • 12
1

I tried this approach, while following the API docs more closely:

- (void)openEmbeddedImagePicker:(UIImagePickerController *)picker {
    [self.navigationController setNavigationBarHidden:YES];
    [self addChildViewController:picker];
    [self.view addSubview:picker.view];
    picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    picker.view.frame = self.view.bounds;
    [picker didMoveToParentViewController:self];
}

But the video recorder's Cancel and playback buttons don't respond. Also, while the containing VC (self in the above method) restricts the orientation to landscape, rotating between the two landscape modes messes up the layout of the picker's overlay bar (on iOS 6, at least).

Does anyone have success restricting the video recorder to landscape mode?

Jerry101
  • 12,157
  • 5
  • 44
  • 63
1

I used following code:

[self.view addSubview:imgPicker.view]; 
[imgPicker viewWillAppear:YES]; 
[imgPicker viewDidAppear:YES];

instead of using presentModalViewController method, [self presentModalViewController:picker animated:NO];

Anomie
  • 92,546
  • 13
  • 126
  • 145
Ronak
  • 165
  • 1
  • 3
  • 15
  • Note that that method of displaying the picker may not be supported. The documentation only refers to presentModalViewController:animated: or UIPopoverController as methods for presenting it. – Anomie Apr 21 '11 at 18:36
  • @ Anomie, I have one question for you, using above code, image picker loading good in landscape mode but I am getting one issue. If I scroll up or down, my application breaks. I am getting error: *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x69959d0> was mutated while being enumerated. I spent almost 5 hr, but didn't fix it. So, can you help me please....... – Ronak Apr 21 '11 at 21:21
0

From the UIImagePickerController Class Reference.

Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

Vaibhav Sharma
  • 1,123
  • 10
  • 22