Save UIImage, Load it in Wrong Orientation

后端 未结 5 1959
无人共我
无人共我 2020-12-13 04:03

I am using the following code to save and load images that I pick from either the library or take using the camera:

//saving an image
- (void)saveImage:(UIIm         


        
相关标签:
5条回答
  • 2020-12-13 04:33

    The root cause is PNG format doesn't have image orientation information, but JPEG format does. So the easiest way to solve this problem is saving your file in JPEG format using UIImageJPEGRepresentation()

    0 讨论(0)
  • 2020-12-13 04:35

    Try to save the image orientation too, then when you load that image, check if that's the correct orientation, and if not, rotate the image with CGAffineTransform or whatever you want.

    0 讨论(0)
  • 2020-12-13 04:35

    You can use Image I/O to save PNG image to file(or NSMutableData) with respect to the orientation of the original image. In the example below I save the PNG image to a file at path.

    - (BOOL)savePngFile:(UIImage *)image toPath:(NSString *)path {
        NSData *data = UIImagePNGRepresentation(image);
        int exifOrientation = [UIImage cc_iOSOrientationToExifOrientation:image.imageOrientation];
        NSDictionary *metadata = @{(__bridge id)kCGImagePropertyOrientation:@(exifOrientation)};
        NSURL *url = [NSURL fileURLWithPath:path];
        CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
        if (!source) {
            return NO;
        }
        CFStringRef UTI = CGImageSourceGetType(source);
        CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, UTI, 1, NULL);
        if (!destination) {
            CFRelease(source);
            return NO;
        }
        CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metadata);
        BOOL success = CGImageDestinationFinalize(destination);
        CFRelease(destination);
        CFRelease(source);
        return success;
    }
    

    cc_iOSOrientationToExifOrientation: is a method of UIImage category.

    + (int)cc_iOSOrientationToExifOrientation:(UIImageOrientation)iOSOrientation {
        int exifOrientation = -1;
        switch (iOSOrientation) {
            case UIImageOrientationUp:
                exifOrientation = 1;
                break;
    
            case UIImageOrientationDown:
                exifOrientation = 3;
                break;
    
            case UIImageOrientationLeft:
                exifOrientation = 8;
                break;
    
            case UIImageOrientationRight:
                exifOrientation = 6;
                break;
    
            case UIImageOrientationUpMirrored:
                exifOrientation = 2;
                break;
    
            case UIImageOrientationDownMirrored:
                exifOrientation = 4;
                break;
    
            case UIImageOrientationLeftMirrored:
                exifOrientation = 5;
                break;
    
            case UIImageOrientationRightMirrored:
                exifOrientation = 7;
                break;
    
            default:
                exifOrientation = -1;
        }
        return exifOrientation;
    }
    

    You can alternatively save the image to NSMutableData using CGImageDestinationCreateWithData and pass NSMutableData instead of NSURL in CGImageDestinationCreateWithURL.

    0 讨论(0)
  • 2020-12-13 04:41

    Check the EXIF information for the image that you're loading, there should be a tag for orientation. Use the value to rotate your image to proper orientation.

    This website should get you started.

    http://www.impulseadventure.com/photo/exif-orientation.html

    (The application you used to view the photo must have this built in, that's why you see correct orientation when opening the photo)

    0 讨论(0)
  • 2020-12-13 04:44

    I have faced similar problem and here is how I solved it.

    While we save image we need to save its orientation information along with the image ...

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setInteger:[myImage imageOrientation] forKey:@"kImageOrientation"];
    [imageOrientation release];
    

    And we load image we need to read its orientation information and apply it to the image…

    UIImage *tempImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    UIImage *orientedImage= [[UIImage alloc] initWithCGImage: tempImage.CGImage scale:1.0 orientation:imageOrientation];
    [tempImage release];
    

    orientedImage is what we need.

    Thanks,

    0 讨论(0)
提交回复
热议问题