2012-07-02 69 views
2

我做了一個iphone應用程序來捕捉相機的圖像,並在下一個視圖中設置該圖像。但問題是圖像旋轉。即風景圖像變成potraite,並且protraite圖像變成風景。 我提到了很多代碼,但無法獲得解決方案。iphone:從相機拍攝的圖像改變方向

我的代碼是:

- (void)btnCapturePressed 
{ 
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
     { 

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

      [picker setAllowsEditing:YES]; 

      picker.sourceType=UIImagePickerControllerSourceTypeCamera; 
      //[self.navigationController pushViewController:(UIViewController *)ipc animated:YES]; 
      [self presentModalViewController:picker animated:YES]; 
      [picker release]; 
     } 
} 

-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo :(NSDictionary *)info 
{ 
      UIImage *imageToScale=[info objectForKey:UIImagePickerControllerOriginalImage]; 

      imgView = [[UIImageView alloc] initWithImage:imageToScale]; 

      [picker presentModalViewController:cropper animated:YES]; 
} 

我也曾涉及過link。有同樣的問題,但找不到解決方案。

請幫幫我。

謝謝。

+0

你的圖像是否旋轉到某個特定的程度? –

+1

所以你必須讓他們再旋轉-90再顯示這將解決你的問題,如果你想要如何做到這一點,只是要求我的幫助 –

+0

如果你需要代碼它只是要求它我會給你它的答案 –

回答

4

所以對於以圖像的時間採取存儲設備的方向並將它傳遞給下面的參數 這裏只是給出任何名稱方法的方法和傳遞參數orientation

switch (orientation) { 
      case UIDeviceOrientationPortrait: 
       [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(0.))]; 
       break; 
      case UIDeviceOrientationPortraitUpsideDown: 
       [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(180.))]; 
       break; 
      case UIDeviceOrientationLandscapeLeft: 
       [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(90.))]; 
       break; 
      case UIDeviceOrientationLandscapeRight: 
       [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.))]; 
       break; 
      case UIDeviceOrientationFaceUp: 
      case UIDeviceOrientationFaceDown: 
      default: 
       break; // leave the layer in its last known orientation 
     } 

和宏我在這裏DegreesToRadians()採用的是如下

static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI/180;}; 

這肯定會工作。

編碼快樂:)

編輯

如果上面的代碼無法正常工作,然後用這一個

@interface UIImage (RotationMethods) 
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees; 
@end 

@implementation UIImage (RotationMethods) 

- (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; 

} 
@end 

,然後調用上面的函數如下

switch (orientation) { 
     case UIDeviceOrientationPortrait: 
      image = [image imageRotatedByDegrees:0]; 
      break; 
     case UIDeviceOrientationPortraitUpsideDown: 
      image = [image imageRotatedByDegrees:180]; 
      break; 
     case UIDeviceOrientationLandscapeLeft: 
      image = [image imageRotatedByDegrees:-90]; 
      break; 
     case UIDeviceOrientationLandscapeRight: 
      image = [image imageRotatedByDegrees:90]; 
      break; 
     case UIDeviceOrientationFaceUp: 
     case UIDeviceOrientationFaceDown: 
     default: 
      break; // leave the layer in its last known orientation 
    } 

如果圖像不在請求中紅方向然後將90加到所有上述imageRotatedByDegrees的論點(即,如果它是0,那麼它將是0 + 90)或按照你的要求。

編輯1

UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation]; 
+0

在拍攝圖像時只是通過設備方向作爲參數 –

+0

featureLayer是要旋轉的圖像 –

+1

好的我不知道爲什麼但編輯的答案會幫助你。只需將編輯後的答案的「@工具」和「@界面」部分,第一個代碼部分照原樣放到要調用圖像旋轉的.m文件中,然後按照答案中提及的要求調用所需度數的開關這肯定會在我的項目 –

1

我參考了蘋果的論壇(https://discussions.apple.com/thread/1537011?start=0&tstart=0)的討論,發現以下解決方案;它對我來說非常合適。我只是複製粘貼它,它像寶石一樣工作;

-(UIImage *) scaleAndRotateImage(UIImage *)image 
{ 
int kMaxResolution = 320; // Or whatever 

CGImageRef imgRef = image.CGImage; 

CGFloat width = CGImageGetWidth(imgRef); 
CGFloat height = CGImageGetHeight(imgRef); 


CGAffineTransform transform = CGAffineTransformIdentity; 
CGRect bounds = CGRectMake(0, 0, width, height); 
if (width > kMaxResolution || height > kMaxResolution) { 
CGFloat ratio = width/height; 
if (ratio > 1) { 
bounds.size.width = kMaxResolution; 
bounds.size.height = bounds.size.width/ratio; 
} 
else { 
bounds.size.height = kMaxResolution; 
bounds.size.width = bounds.size.height * ratio; 
} 
} 

CGFloat scaleRatio = bounds.size.width/width; 
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
CGFloat boundHeight; 
UIImageOrientation orient = image.imageOrientation; 
switch(orient) { 

case UIImageOrientationUp: //EXIF = 1 
transform = CGAffineTransformIdentity; 
break; 

case UIImageOrientationUpMirrored: //EXIF = 2 
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
transform = CGAffineTransformScale(transform, -1.0, 1.0); 
break; 

case UIImageOrientationDown: //EXIF = 3 
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
transform = CGAffineTransformRotate(transform, M_PI); 
break; 

case UIImageOrientationDownMirrored: //EXIF = 4 
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
transform = CGAffineTransformScale(transform, 1.0, -1.0); 
break; 

case UIImageOrientationLeftMirrored: //EXIF = 5 
boundHeight = bounds.size.height; 
bounds.size.height = bounds.size.width; 
bounds.size.width = boundHeight; 
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
transform = CGAffineTransformScale(transform, -1.0, 1.0); 
transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
break; 

case UIImageOrientationLeft: //EXIF = 6 
boundHeight = bounds.size.height; 
bounds.size.height = bounds.size.width; 
bounds.size.width = boundHeight; 
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
break; 

case UIImageOrientationRightMirrored: //EXIF = 7 
boundHeight = bounds.size.height; 
bounds.size.height = bounds.size.width; 
bounds.size.width = boundHeight; 
transform = CGAffineTransformMakeScale(-1.0, 1.0); 
transform = CGAffineTransformRotate(transform, M_PI/2.0); 
break; 

case UIImageOrientationRight: //EXIF = 8 
boundHeight = bounds.size.height; 
bounds.size.height = bounds.size.width; 
bounds.size.width = boundHeight; 
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
transform = CGAffineTransformRotate(transform, M_PI/2.0); 
break; 

default: 
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 

} 

UIGraphicsBeginImageContext(bounds.size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
CGContextTranslateCTM(context, -height, 0); 
} 
else { 
CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
CGContextTranslateCTM(context, 0, -height); 
} 

CGContextConcatCTM(context, transform); 

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return imageCopy; 
} 
+0

謝謝,這對我有很大幫助 - 特別是在展示如何提取方向信息的地方從圖像! – codehearted

2

我這個問題苦苦掙扎,但發現了一個更容易的解決方案:

AVCaptureConnection *vc = [self.snapper connectionWithMediaType:AVMediaTypeVideo]; 

    // THIS IS THE KEY LINE!!!!!!!!!!!!!!!!!!!!! 
    [vc setVideoOrientation:[self interfaceToVideoOrientation]]; 

    [self.snapper captureStillImageAsynchronouslyFromConnection:vc 
              completionHandler: 
    ^(CMSampleBufferRef buf, NSError *err) { 
    NSData* data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buf]; 
    UIImage* im = [UIImage imageWithData:data]; 
    // Do something with the image. 
    }]; 


- (AVCaptureVideoOrientation)interfaceToVideoOrientation 
{ 
    switch ([UIApplication sharedApplication].statusBarOrientation) 
    { 
    case UIInterfaceOrientationPortrait: 
     return AVCaptureVideoOrientationPortrait; 
    case UIInterfaceOrientationPortraitUpsideDown: 
     return AVCaptureVideoOrientationPortraitUpsideDown; 
    case UIInterfaceOrientationLandscapeRight: 
     return AVCaptureVideoOrientationLandscapeRight; 
    case UIInterfaceOrientationLandscapeLeft: 
     return AVCaptureVideoOrientationLandscapeLeft; 
    default: 
     return AVCaptureVideoOrientationPortrait; 
    } 
} 

設置視頻取向之前捕獲現擁有固定資產我的問題與圖像走出了錯誤的方向。

+0

在小時後處理這個定位問題後,你終於幫助了我。驚人!!非常感謝你,我的應用現在可以運行。 – owlswipe

相關問題