iPhone/Xcode: UIImage and UIImageView won't get with the program -


i attempting save , load uiimage , iphone documents directory after image picked iphone photo library. able so, reason, when load image, rotates 90 degrees counterclockwise. here saveimage , 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 loading image this, see if would, crazy reason, solve problem (it didn't, obviously):

uiimage* theimage = [[uiimage alloc] initwithdata:[self loadimage]]; 

i have done testing have 2 uiimageviews side-by-side, 1 on left doesn't save , load image before display (it straight-up shows image once photo picked imagepicker), , 1 on right displays after save load occurs. 1 on right 1 rotating 90 degrees ccw; non save/load picture displaying correctly. occurs on actual iphone. simulator shows both images correctly. have spent many, many hours attempting figure out, no avail. ideas causing this?

edit: should known image says it's oriented up, when isn't! rotates after of code has been run. i've tried doing force redraw in between code see if changes things, doesn't (though possible i'm doing wrong. knows, anymore). also, not autorotate photos have been screenshot iphone. photos have been taken camera. [i haven't tried downloaded photos.] it's weirdest thing...

and if you're wondering how i'm pulling picture, here's method @ least shed light on i'm doing (it's not every method needed function, obviously, gives insight on how i've written code):

- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info {  theimageview.image = [info objectforkey:@"uiimagepickercontrolleroriginalimage"];  [self saveimage:[info objectforkey:@"uiimagepickercontrolleroriginalimage"]];  [picker dismissmodalviewcontrolleranimated:yes];  } 

any advice appreciated. , know much: if there's way crazy stupid, never-before-heard-of problem, chances are, i'll find it, apparently :p

ok. figured out. apparently when save things png representation, , not jpeg representation, image orientation information not saved it. because of this, every image loaded default showing orientation up. so, easiest way 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 picture names .png .jpg. 0.0 part of code above control alpha levels, required of jpegs. hope helps people in future!


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -