Camera image changes orientation

后端 未结 2 1989
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 00:22

I been trying to upload camera images from iPhone to my web server but some how the images change it\'s orientation after wards. This only happens with camera images and not

相关标签:
2条回答
  • 2020-12-09 00:50

    iPhone images are always stored the same way regardless of how the phone is held, but a flag is set in the EXIF data that specifies which orientation the image should be in. Almost all native OSX applications such as iPhoto and Preview can read this EXIF orientation tag correctly and rotates the image automatically, but almost all Windows applications and web browsers don't take the orientation EXIF tag into account. You'll have to manually rotate the image on the web server before saving it. I don't know which web server technology you use, but the C# code to do this is:

    public static void FixOrientation(this Image image)
    {
        // 0x0112 is the EXIF byte address for the orientation tag
        if (!image.PropertyIdList.Contains(0x0112))
        {
            return;
        }
    
        // get the first byte from the orientation tag and convert it to an integer
        var orientationNumber = image.GetPropertyItem(0x0112).Value[0];
    
        switch (orientationNumber)
        {
            // up is pointing to the right
            case 8:
                image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;
            // up is pointing to the bottom (image is upside-down)
            case 3:
                image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                break;
            // up is pointing to the left
            case 6:
                image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;
            // up is pointing up (correct orientation)
            case 1:
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-09 00:51

    You can get the image orientation of a UIImage by using the imageOrientation method. So for example, you want to save the image taken from the camera you can do:

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (image) {
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageToSavedPhotosAlbum:[image CGImage] 
                                  orientation:[image imageOrientation]
                              completionBlock:^(NSURL *assetURL, NSError *error){ /* error handling */ }];
        [library autorelease];
    }
    

    and then when you want to get the UIImage from the library you can retrieve the orientation by doing something like:

    UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage] scale:1.0 orientation:[asset orientation]]; 
    
    0 讨论(0)
提交回复
热议问题