gpt4 book ai didi

ios - UIImageView 没有在 iPhone 中以正确的方向呈现图像

转载 作者:行者123 更新时间:2023-11-29 01:59:24 27 4
gpt4 key购买 nike

我尝试了很多解决方案,但还没有奏效

这是我正在做的:

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
//imagePicker.allowsEditing = YES;

NSOperatingSystemVersion ios8_0_0 = (NSOperatingSystemVersion){8, 0, 0};

//This was suggested in answer of some question of stackoverflow
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_0])
{
if([[UIDevice currentDevice]orientation] == UIDeviceOrientationFaceUp)
{
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:@"orientation"];
}
else
{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
}
}
}

self.imagePickerController = imagePicker;
[self presentViewController:self.imagePickerController animated:YES completion:nil];

}

当上述方法不起作用时,我在保存图片时尝试了一些其他方法

 NSData *imgPngData = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage]);
UIImage *image = [UIImage imageWithData:imgPngData];
imgPngData = UIImagePNGRepresentation(image);
[_profilePicture setImage:[UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:UIImageOrientationUp]];

但对行为没有影响。这就是我拍摄这张照片时发生的事情。 Iam taking this image

这就是出现在我的 imageView 上的内容 enter image description here

请提出一些建议。提前感谢大家。

最佳答案

我通过集成以下代码解决了同样的问题。引用 link对于代码

- (UIImage *)fixOrientation {

// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp) return self;

// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;

switch (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;

case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;

case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, self.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}

switch (self.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;

case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, self.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
// CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
// CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
// 8, self.size.width,
// colorspace,
// kCGImageAlphaNone);
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
break;

default:
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}

关于ios - UIImageView 没有在 iPhone 中以正确的方向呈现图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30522683/

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