简体   繁体   English

UIImageView不能以正确的方向在iPhone中显示图像

[英]UIImageView not presenting image in correct orientation in iPhone

I have tried alot of solutions but non has worked yet 我已经尝试了很多解决方案,但尚未奏效

Heres what i am doing: 这是我在做什么:

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

}

When the above didnt worked i tried some thing else while saving picture 当上述方法不起作用时,我在保存图片时尝试了其他方法

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

but no effect on behavior. 但对行为没有影响。 This is what happening Iam taking this image. 这就是Iam拍摄这张图片时发生的事情。 我拍这张照片

And this what appearing on my imageView 而这出现在我的imageView上 在此处输入图片说明

Kindly suggest some thing. 请提出一些建议。 Thanking all of you in advance. 预先感谢大家。

I have same problem solved it by integrating below code. 我通过集成下面的代码解决了同样的问题。 Reference link for the code 代码的参考链接

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

Use this may help you 使用此功能可能对您有帮助

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {

        [uploadImageView setContentMode:UIViewContentModeScaleAspectFit];
        uploadImageView.layer.masksToBounds = YES;
        uploadImage=[info objectForKey:UIImagePickerControllerEditedImage];
        uploadImage = [self imageWithImage:uploadImage scaledToWidth:uploadImageView.frame.size.width];
        uploadImageView.image=uploadImage;

        [picker dismissViewControllerAnimated:YES completion:nil];

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM