【问题标题】:UIImageView not presenting image in correct orientation in iPhoneUIImageView 没有在 iPhone 中以正确的方向呈现图像
【发布时间】:2015-05-29 06:35:10
【问题描述】:

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

这是我在做什么:

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

但对行为没有影响。这就是我正在拍摄这张照片的情况。

这就是我的 imageView 上出现的内容

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

【问题讨论】:

    标签: ios objective-c iphone uiimageview orientation


    【解决方案1】:

    我通过集成以下代码解决了同样的问题。代码参考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;
    }
    

    【讨论】:

    • 为什么正常功能对任何人都不起作用?是否有充分的理由实施此调整?
    • 它下面的cmets不是很好,所以很多人写了关于应用程序由于内存而崩溃。你是否也面临同样的问题?
    • 是的,如果您尝试多次拍摄照片,那么应用程序可能会崩溃。坦率地说,不知道执行此操作的确切原因...试图找到根本原因,但没有哪里都找不到。。如果你在哪里找到,请与我分享。
    • 我已经尝试了苹果为旋转提供的几乎所有功能,但对我来说运气不好,我想还没有任何效果。当我尝试使用它时,您是如何实现这一点的,它有很多修复并警告它甚至不允许我尝试这个
    • 它的类别到 UIImage 类..在这里上传了一个代码dropbox.com/s/dxd2aebvv8h9nh9/Orientation.zip?dl=0..
    【解决方案2】:

    使用它可以帮助你

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2012-02-10
      • 2014-03-22
      • 2013-05-20
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多