iPhone:以纵向模式捕获的图像在UIImageview中以横向模式加载

时间:2012-01-09 10:26:25

标签: iphone objective-c uiimageview uiimagepickercontroller

我正在开发一个应用程序,我以纵向模式捕获图像,然后移动到另一个带有图像数据的类,并在UIScrollView上的UIImageView中显示该图像。

现在如果我正在使用图像表单库,它工作正常,但如果我必须捕获图像,它将以横向模式显示数据。下面是我的两个类的代码。 :

第1课:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSData *imgData = UIImagePNGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"]);

    [picker dismissModalViewControllerAnimated:YES];
        image = [[UIImage alloc] initWithData:imgData];

    CGImageRef imgRefCrop = image.CGImage;
            UIImage *photo = [UIImage imageWithCGImage:imgRefCrop];

    PhotoCropperPage *photoCropper = [[PhotoCropperPage alloc] 
                                           initWithPhoto:photo
                                                 delegate:self                                                  
                uiMode:PCPUIModePresentedAsModalViewController
                                          showsInfoButton:YES];

    [photoCropper setMinZoomScale:0.3f];
    [photoCropper setMaxZoomScale:4.0f];
    [self.navigationController pushViewController:photoCropper animated:YES];
}

第2课:

- (void) loadPhoto
{
    if (self.photo == nil) {
        return;
    }

    CGFloat w = self.photo.size.width;
    CGFloat h = self.photo.size.height;

    CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
    self.scrollView.contentSize = imageViewFrame.size;

    UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
    iv.image = self.photo;
    [self.scrollView addSubview:iv];
    self.imageView = iv;
    [iv release];
}

我没有得到我犯错误的地方?任何人都可以指出我吗?提前谢谢。

2 个答案:

答案 0 :(得分:7)

刚刚找到了一个很棒的答案。这是链接:https://stackoverflow.com/a/5427890/1047258

然后你可以这样做:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  UIImage *theImage = [[info objectForKey:@"UIImagePickerControllerOriginalImage"]fixOrientation];
  //rest of your code
}

并且图像将是正确方向的图像。它在我的项目中起作用

希望它有所帮助。

答案 1 :(得分:0)

小心不要丢弃方向数据......试试这个:

image = [[UIImage alloc] initWithData:imgData];

CGImageRef imgRefCrop = image.CGImage;
UIImage *photo = [UIImage imageWithCGImage:imgRefCrop scale:image.scale orientation:image.orientation ];

另外,您需要确保根据图像的方向裁剪正确的像素。