2013年1月19日 星期六

UIImagePickerController在IOS6.0上的Bug

UIImagePickerController在iOS6.0 的iPad上,不能只用在橫向的設計上,會導致Crash,其原因可以看
https://devforums.apple.com/message/731764#731764

裡面有詳細的說明,這的錯誤點是iOS6.0 SDK的原生錯誤,而此錯誤只會出現在不要旋轉模式的橫向App。以下是解決方法。

1. 沿用前一篇UIImagePickerController的例子,將方向設成橫向

2. 加入新的class檔,這是為了要避開原生的UIImagePickerController錯誤。

3. noRotationImagePicker.m上加入避開錯誤的程式碼

#import "noRotationImagePicker.h"

@interface noRotationImagePicker ()

@end

@implementation noRotationImagePicker

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotate
{
    return NO;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}


4. 在mainViewController.m上使用的地方,改成呼叫自己所改的Class

- (IBAction)openPhotoLibrary:(id)sender {
  
    //UIView *anchor = sender;
       
    UIImagePickerController *m_imagePicker = [[noRotationImagePicker alloc] init];
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypePhotoLibrary]) {
      
        m_imagePicker.wantsFullScreenLayout = YES;
        m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        m_imagePicker.delegate = self;
        [m_imagePicker setAllowsEditing:YES]; // 圖片出現可編輯的選項
      
        popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
      
        [popover setPopoverContentSize:CGSizeMake(300, 300)];
               
        [popover presentPopoverFromRect:CGRectMake(0,0,300,300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; // 此處popover的位置改成自定       
      
    }

}
- (IBAction)openPhotoAlbum:(id)sender {
  
    UIView *anchor = sender;
    UIImagePickerController *m_imagePicker = [[noRotationImagePicker alloc] init];
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
      
      
        m_imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        m_imagePicker.delegate = self;
        [m_imagePicker setAllowsEditing:YES];
      
        popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
      
        [popover setPopoverContentSize:CGSizeMake(300, 300)];
      
        [popover presentPopoverFromRect:anchor.frame inView:anchor.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
      
    }
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageView setImage:image];
   
     [popover dismissPopoverAnimated:YES]; // 使用正確的popover dismiss
   
}