【问题标题】:iPhone : Image Captured in portrait mode is loaded in landscape mode in UIImageviewiPhone:以纵向模式捕获的图像在 UIImageview 中以横向模式加载
【发布时间】:2012-01-09 10:26:25
【问题描述】:

我正在开发一个应用程序,我在其中以纵向模式捕获图像,然后使用图像数据移动到另一个类并在 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];
}

我没有找到我做错的地方?有人可以指出我吗?提前致谢。

【问题讨论】:

标签: iphone objective-c uiimageview uiimagepickercontroller


【解决方案1】:

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

然后你可以这样做:

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

并且 theImage 将是您具有正确方向的图像。它在我的项目中有效

希望对你有帮助。

【讨论】:

    【解决方案2】:

    注意不要丢弃方向数据...试试这个:

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

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

    【讨论】:

    • 它不允许我用 image.orientation 设置方向。
    • 不,您通过调用+[UIImage imageWithCGImage:scale:orientation:] 而不是+[UIImage imageWithCGImage:] 来设置新UIImage 的方向。如果您不能这样做,则必须先旋转/翻转您的 UIImage,然后再将其渲染到您的 CGContext
    • 是的,这就是我正在做的事情。它允许我设置比例但是在方向字段中我写图像。 ,那么在点(。)符号之后没有像方向这样的选项
    • 该属性名为imageOrientation。详情请参阅documentation
    • 您是否要裁剪使用相机拍摄的图像?看到这个答案:stackoverflow.com/questions/7203847/…
    猜你喜欢
    • 2013-05-12
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多