2013-07-29 30 views
1

我使用UIImagePickerController從圖片庫中選取圖像。我使用風景和肖像編輯圖像。當我選擇一張圖像時,它將顯示正確的人像方向,但在橫向模式下,它會顯示上下顛倒。我提到了一些代碼。但仍然無法正常工作。我提到這個鏈接iphone : image captured from camera changes orientation拍攝圖像後圖像上下顛倒

代碼:

-(void)pick:(id)sender{ 


      imagePicker=[[UIImagePickerController alloc]init]; 

      imagePicker.delegate = self; 

      imagePicker.allowsEditing =NO; 

      imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

      // imagePicker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum; 


      // [self presentModalViewController:imagePicker animated:YES]; 

      [self presentViewController:imagePicker animated:YES completion:nil]; 

} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 


newImage = [[UIImageView alloc] initWithImage:[info objectForKey:UIImagePickerControllerOriginalImage]]; 




    [newImage setFrame:CGRectMake(0, 0, 320, 568)]; 

    [newImage setUserInteractionEnabled:YES]; 

[self.view addSubview:newImage]; 

    [picker dismissViewControllerAnimated:YES completion:nil]; 

UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation]; 

    switch (curDeviceOrientation) { 
     case UIDeviceOrientationPortrait: 

      NSLog(@"port"); 
      newImage.image = [ newImage.image imageRotatedByDegrees:0]; 
      break; 
     case UIDeviceOrientationPortraitUpsideDown: 
      newImage.image = [ newImage.image imageRotatedByDegrees:180]; 
      break; 
     case UIDeviceOrientationLandscapeLeft: 

      NSLog(@"Left"); 
      newImage.image = [ newImage.image imageRotatedByDegrees:0]; 
      break; 
     case UIDeviceOrientationLandscapeRight: 
      newImage.image = [ newImage.image imageRotatedByDegrees:90]; 
      break; 
     case UIDeviceOrientationFaceUp: 
     case UIDeviceOrientationFaceDown: 
     default: 
      break; // leave the layer in its last known orientation 
    } 

} 

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees 
{ 
    // calculate the size of the rotated view's containing box for our drawing space 
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)]; 

    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); 
    rotatedViewBox.transform = t; 
    CGSize rotatedSize = rotatedViewBox.frame.size; 

    // Create the bitmap context 
    UIGraphicsBeginImageContext(rotatedSize); 
    CGContextRef bitmap = UIGraphicsGetCurrentContext(); 

    // Move the origin to the middle of the image so we will rotate and scale around the center. 
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

    // // Rotate the image context 
    CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); 

    // Now, draw the rotated/scaled image into the context 
    CGContextScaleCTM(bitmap, 1.0, -1.0); 
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2, self.size.width, self.size.height), [self CGImage]); 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 

回答

0

試試這個。

在你的類,

@interface NonRotatingUIImagePickerController : UIImagePickerController 

@end 

@implementation NonRotatingUIImagePickerController 

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

@implementation YOUR_CLASS 

UIImagePickerController *imgPicker; 

- (void)viewDidLoad 
{ 
    imgPicker = [[NonRotatingUIImagePickerController alloc] init]; 
} 

-(IBAction)clickForPhoto:(id)sender 
{ 
    imgPicker.delegate = self; 

    imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentViewController:imgPicker animated:YES completion:nil]; 
} 

希望這會幫助你。

謝謝。

+0

不適合我 – MobileMon

1

經過一番研究後,如果您使用向上指向的音量按鈕(自然方式,imo)拍攝照片,則橫向圖像將顯示爲顛倒。這是由於相機的傳感器不知道您的手機是橫向還是橫向。它建立在假設「直立」方向是音量按鈕指向下方的方向上。