根据设备方向保存照片

时间:2011-06-11 12:49:04

标签: iphone objective-c uiimagepickercontroller

以下代码为我提供了纵向模式的照片,无论其方向如何。如何确保照片符合当前方向?

-(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图像。

1 个答案:

答案 0 :(得分:0)

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

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

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

另请查看我对相关问题的答案(如何将其设为仅限肖像或仅限景观),它也可能对您有所帮助 UIImagePickerController - SourceType Camera - Allow taking photo only portrait orientation