Updating UIImage orientation metaData?

前端 未结 2 2039
不思量自难忘°
不思量自难忘° 2020-12-29 10:37

Basic Task: update the EXIF orientation property for the metaData asociated with a UIImage. My problem is that I don\'t know where the orientation property

相关标签:
2条回答
  • 2020-12-29 10:38

    Take a look at my answer here:
    Force UIImagePickerController to take photo in portrait orientation/dimensions iOS

    and associated project on github (you won't need to run the project, just look at the readme).

    It's more concerned with reading rather than writing metadata - but it includes a few notes on Apple's imageOrientation and the corresponding orientation 'Exif' metadata.

    This might be worth a read also
    Captured photo automatically rotated during upload in IOS 6.0 or iPhone

    There are two different constant numbering conventions in play to indicate image orientation.

    1. kCGImagePropertyOrientation constants as used in TIFF/IPTC image metadata tags
    2. UIImageOrientation constants as used by UIImage imageOrientation property.

    iPhones native camera orientation is landscape left (with home button to the right). Native pixel dimensions always reflect this, rotation flags are used to orient the image correctly with the display orientation.

    Apple            UIImage.imageOrientation     TIFF/IPTC kCGImagePropertyOrientation
    
    iPhone native     UIImageOrientationUp    = 0  =  Landscape left  = 1  
    rotate 180deg     UIImageOrientationDown  = 1  =  Landscape right = 3  
    rotate 90CCW      UIImageOrientationLeft  = 2  =  Portrait  down  = 8  
    rotate 90CW       UIImageOrientationRight = 3  =  Portrait  up    = 6  
    

    UIImageOrientation 4-7 map to kCGImagePropertyOrientation 2,4,5,7 - these are the mirrored counterparts.

    UIImage derives it's imagerOrientation property from the underlying kCGImagePropertyOrientation flags - that's why it is a read-only property. This means that as long as you get the metadata flags right, the imageOrientation will follow correctly. But if you are reading the numbers in order to apply a transform, you need to be aware which numbers you are looking at.

    0 讨论(0)
  • 2020-12-29 11:04

    A few gleanings from my world o' pain in looking into this:

    Background: Core Image face detection was failing and it seemed to be related to using featuresInImage:options: and using the UIImage.imageOrientation property as an argument. With an image adjusted to have no rotation and not mirrored, detection worked fine but when passing in an image directly from the camera detection failed.

    Well...UIImage.imageOrientation is DIFFERENT than the actual orientation of the image.

    In other words...

    UIImage* tmpImage = [self.imageInfo objectForKey:UIImagePickerControllerOriginalImage];
    printf("tmpImage.imageOrientation: %d\n", tmpImage.imageOrientation);
    

    Reports a value of 3 or UIImageOrientationRight whereas using the metaData returned by the UIImagePickerControllerDelegate method...

       NSMutableDictionary* metaData = [[tmpInfo objectForKey:@"UIImagePickerControllerMediaMetadata"] mutableCopy];
       printf(" metaData orientation    %d\n", [[metaData objectForKey:@"Orientation"] integerValue]);
    

    Reports a value of 6 or UIImageOrientationLeftMirrored.

    I suppose it seems obvious now that UIImage.imageOrientation is a display orientation which appears to determined by source image orientation and device rotation. (I may be wrong here) Since the display orientation is different than the actual image data, using that will cause the CIDetector to fail. Ugh.

    I'm sure all of that serves very good and important purposes for liquid GUIs etc. but it is too much to deal with for me since all the CIDetector coordinates will also be in the original image orientation making CALayer drawing sick making. So, for posterity here is how to change the Orientation property of the metaData AND the image contained therein. This metaData can then be used to save the image to the cameraRoll.

    Solution

    // NORMALIZE IMAGE
    UIImage* tmpImage = [self normalizedImage:[self.imageInfo objectForKey:UIImagePickerControllerOriginalImage]];
    NSMutableDictionary * tmpInfo =[self.imageInfo mutableCopy];
    NSMutableDictionary* metaData = [[tmpInfo objectForKey:@"UIImagePickerControllerMediaMetadata"] mutableCopy];
    
    [metaData setObject:[NSNumber numberWithInt:0] forKey:@"Orientation"];
    [tmpInfo setObject:tmpImage forKey:@"UIImagePickerControllerOriginalImage"];
    [tmpInfo setObject:metaData forKey:@"UIImagePickerControllerMediaMetadata"];
    
    self.imageInfo = tmpInfo;
    
    0 讨论(0)
提交回复
热议问题