iphone:从相机拍摄的图像改变方向 [英] iphone : image captured from camera changes orientation

查看:90
本文介绍了iphone:从相机拍摄的图像改变方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个iPhone应用程序来捕捉相机中的图像并在下一个视图中设置该图像。但问题是图像是旋转的。即景观图像成为potraite和protraite图像成为景观。
我已经提到了许多代码但无法获得解决方案。

I made an iphone app to capture image from camera and to set that image in next view. But the problem is that image is rotated. i.e landscape image becomes potraite and protraite image becomes landscape. I have referred many codes but could not get solution.

我的代码是:

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

我还提到了链接。同样的问题,但找不到解决方案。

I have also refered the link. with same problem, but could not find the solution.

请帮帮我。

谢谢。

推荐答案

因此,在图像时,请存储设备的方向并将其作为参数传递给下面的方法
这里只是给方法命名并传递参数 orientation

So for that at the time of image take store the orientation of the device and pass it to the method below as parameter Here just give any name to method and pass the parameter 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()如下

and the macro I have used here DegreesToRadians() is as follow

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

这肯定有效。

快乐编码:)

编辑

如果上述代码不能正常工作然后使用这个

If the above code doesn't work well then use this one

@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

然后调用上面的函数如下所示

and then call the above function as below

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
    }   

如果图像不在所需的方向然后在上面的所有 imageRotatedByDegrees 的参数中添加90(即如果它是0则为0 + 90)或者根据需要添加。

If the image is not in required orientation then add 90 to all of the above imageRotatedByDegrees's argument (i.e. if it is 0 then it will be 0+90) or as you required.

编辑1

UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];

这篇关于iphone:从相机拍摄的图像改变方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆