Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953462
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:58:40+00:00 2026-05-20T01:58:40+00:00

I am attempting to save and load a UIImage to and from the iPhone

  • 0

I am attempting to save and load a UIImage to and from the iPhone documents directory after the image is picked from the iPhone Photo Library. It is able to do so, but for some reason, when I load the image, it rotates it 90 degrees counterclockwise. Here is my saveImage and loadImage methods:

Save Image:

- (void)saveImage: (UIImage*)image{
if (image != nil)
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithString: @"lePhoto.png"] ];
    //NSData* data = UIImagePNGRepresentation(image);
    //[data writeToFile:path atomically:YES];
    [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
    }
}

Load Image:

- (NSData*)loadImage{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                  [NSString stringWithString: @"lePhoto.png"] ];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];


//UIImage *image = [[UIImage alloc] initWithData:data];   //my second try
//UIImage* image = [UIImage imageWithContentsOfFile:path];   //first try
//return image;
return data;
[data release];
}

And I am now loading the image like this, to see if it would, for some crazy reason, solve my problem (it didn’t, obviously):

UIImage* theImage = [[UIImage alloc] initWithData:[self loadImage]];

I have done some testing where I have two UIImageViews side-by-side, one on the left that doesn’t save and load the image before display (it just straight-up shows the image once a photo is picked from the ImagePicker), and one on the right that displays AFTER the save load occurs. The one on the right is the only one rotating 90 degrees CCW; the non Save/Load picture is displaying correctly. This only occurs on the actual iPhone. The simulator shows both images correctly. I have spent many, many hours attempting to figure it out, but to no avail. Any ideas as to what could be causing this?

EDIT: It should also be known that the image always says it’s oriented up, even when it obviously isn’t! It only rotates after ALL of the code has been run. I’ve tried doing a force redraw in between my code to see if that changes things, but it doesn’t (though it is possible I’m even doing that wrong. Who knows, anymore).
Also, it will not autorotate photos that have been screenshot from the iPhone. Only photos that have been taken by the camera. [I haven’t tried downloaded photos.] It’s the weirdest thing…

And if you’re wondering exactly how I’m pulling my picture, here’s the method that will at least shed light on what I’m doing (it’s not every method needed to do this function, obviously, but it gives insight on how I’ve written my code):

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
[self saveImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"]]; 
[picker dismissModalViewControllerAnimated:YES]; 
}

Any advice is greatly appreciated. And I do know this much: if there’s a way to do something crazy to get some stupid, never-before-heard-of problem, chances are, I’ll find it, apparently 😛

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:58:41+00:00Added an answer on May 20, 2026 at 1:58 am

    Ok. I figured it out. Apparently when you save things with PNG representation, and not JPEG representation, the image orientation information is not saved with it. Because of this, every image loaded will default to showing orientation up. So, the easiest way is to do this:

    NSData* data = UIImageJPEGRepresentation(image,0.0);
    [data writeToFile:path atomically:YES];
    

    instead of this:

    [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
    

    And, of course, changing all the picture names from .png to .jpg. The 0.0 part of the code above is to control Alpha levels, which is required of jpegs. Hope this helps people in the future!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm opening an image from the gallery, resizing it, and attempting to save the
Im attempting to crop a pretty high res image and save the result to
I'm attempting to use TinyXML to read and save from memory, instead of only
I'm attempting to define an extremely simple utility method that will save me from
I'm attempting to implement a Save/Load feature into my small game. To accomplish this
I am attempting to save recorded audio from an application to the storage device
I'm attempting to save a dataURL made from an HTML5 canvas to my mysql
I am attempting to add an image to each save button on the page.
So, I'm doing some image processing in MATLAB, and attempting to save a file
The below exception is thrown on the iPhone simulator when attempting to save a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.