基于设备方向保存照片

问题描述:

以下代码为我提供了纵向模式下的照片,与方向无关。如何确保照片尊重当前的方向?基于设备方向保存照片

-(void) TakePhotoWithCamera 
{ 
    [self startCameraPickerFromViewController:self usingDelegate:self]; 
} 

- (BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject 
{ 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     picker.allowsImageEditing = YES; 
     picker.delegate = self; 
     [controller presentModalViewController:picker animated:YES]; 
     [picker release]; 
    } 

    return YES; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIImage * img = [info objectForKey:@"UIImagePickerControllerEditedImage"]; 
    [self useImage:img]; 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
} 

-(void)useImage:(UIImage *)theImage { 
    NSData *addImageData = UIImagePNGRepresentation(theImage); 
    // Here we return the Image 
} 

我的应用程序是肖像模式。如果我从图库或相机中选择图像,我会得到像上面编码的Tiles一样的图像。

+0

你有没有看着['this'(HTTP: //*.com/questions/1315251/how-to-rotate-a-uiimage-90-degrees)?您可以根据当前的方向来移动图像的方向。 – 2011-06-11 13:08:52

从你的iPhone相机拍摄的图像将具有方向(UIImageOrientation)信息(EXIF信息)。这正好说明了图像的方向。如果查看器能够理解这些信息,它会正确显示它们,否则你会看到图像(我猜Safari不尊重方向,只是比较开放与safari)。

现在,在图像捕捉之后有一个(伪)标准程序,使其成为默认方向(UIImageOrientationUp)并在内部旋转实际图像 - 这使得图像在任何查看器上均可见。

请检查以下帖子并使用scaleAndRotate函数。这将解决您的问题。 UIImagePickerController camera preview is portrait in landscape app

也看看我的相关问题回答(你怎么让它仅纵向或横向只),它可以帮助你以及 UIImagePickerController - SourceType Camera - Allow taking photo only portrait orientation