gpt4 book ai didi

苹果手机 : image captured from camera changes orientation

转载 作者:行者123 更新时间:2023-12-03 20:21:59 24 4
gpt4 key购买 nike

我制作了一个 iPhone 应用程序来从相机捕获图像并在下一个 View 中设置该图像。但问题是图像被旋转了。即风景图像变成肖像,肖像图像变成风景。我引用了很多代码但无法得到解决方案。

我的代码是:

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

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

请帮助我。

谢谢。

最佳答案

因此,在拍摄图像时,存储设备的方向并将其作为参数传递给下面的方法这里只需给方法命名并传递参数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()如下

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

这肯定有效。

快乐编码:)

编辑

如果上面的代码不能很好地工作,那么使用这个

@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

然后调用上面的函数如下

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
}

如果图像未处于所需的方向,则将 90 添加到上述所有 imageRotatedByDegrees 的参数中(即,如果它是 0,则它将是 0+90)或根据您的需要。

编辑 1

UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];

关于苹果手机 : image captured from camera changes orientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11291344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com