image - Photo uploads from my iOS app are being rotated -
i have ios app building. when upload portrait image server, , server resizes image, image rotated landscape. happens iphone uploads. uploading desktop app works fine. why happening, , how can correct it?
@andew
create 1 category of uiimage.. write code in uiimage+fixorientation.h file
#import <uikit/uikit.h> @interface uiimage (fixorientation) - (uiimage *)fixorientation; @end
and in #import "uiimage+fixorientation.h" file
#import "uiimage+fixorientation.h" @implementation uiimage (fixorientation) - (uiimage *)fixorientation { // no-op if orientation correct if (self.imageorientation == uiimageorientationup) return self; // need calculate proper transformation make image upright. // in 2 steps: rotate if left/right/down, , flip if mirrored. cgaffinetransform transform = cgaffinetransformidentity; switch (self.imageorientation) { case uiimageorientationdown: case uiimageorientationdownmirrored: transform = cgaffinetransformtranslate(transform, self.size.width, self.size.height); transform = cgaffinetransformrotate(transform, m_pi); break; case uiimageorientationleft: case uiimageorientationleftmirrored: transform = cgaffinetransformtranslate(transform, self.size.width, 0); transform = cgaffinetransformrotate(transform, m_pi_2); break; case uiimageorientationright: case uiimageorientationrightmirrored: transform = cgaffinetransformtranslate(transform, 0, self.size.height); transform = cgaffinetransformrotate(transform, -m_pi_2); break; case uiimageorientationup: case uiimageorientationupmirrored: break; } switch (self.imageorientation) { case uiimageorientationupmirrored: case uiimageorientationdownmirrored: transform = cgaffinetransformtranslate(transform, self.size.width, 0); transform = cgaffinetransformscale(transform, -1, 1); break; case uiimageorientationleftmirrored: case uiimageorientationrightmirrored: transform = cgaffinetransformtranslate(transform, self.size.height, 0); transform = cgaffinetransformscale(transform, -1, 1); break; case uiimageorientationup: case uiimageorientationdown: case uiimageorientationleft: case uiimageorientationright: break; } // draw underlying cgimage new context, applying transform // calculated above. cgcontextref ctx = cgbitmapcontextcreate(null, self.size.width, self.size.height, cgimagegetbitspercomponent(self.cgimage), 0, cgimagegetcolorspace(self.cgimage), cgimagegetbitmapinfo(self.cgimage)); cgcontextconcatctm(ctx, transform); switch (self.imageorientation) { case uiimageorientationleft: case uiimageorientationleftmirrored: case uiimageorientationright: case uiimageorientationrightmirrored: // grr... cgcontextdrawimage(ctx, cgrectmake(0,0,self.size.height,self.size.width), self.cgimage); break; default: cgcontextdrawimage(ctx, cgrectmake(0,0,self.size.width,self.size.height), self.cgimage); break; } // , create new uiimage drawing context cgimageref cgimg = cgbitmapcontextcreateimage(ctx); uiimage *img = [uiimage imagewithcgimage:cgimg]; cgcontextrelease(ctx); cgimagerelease(cgimg); return img; } @end
and in delegate method.. use [img **fixorientation**];
method
#pragma mark - camera imagepickercontroller delegates - (void)imagepickercontroller:(uiimagepickercontroller *)picker1 didfinishpickingmediawithinfo:(nsdictionary *)info { [picker1 dismissviewcontrolleranimated:yes completion:nil]; nsstring *mediatype = info[uiimagepickercontrollermediatype]; if ([mediatype isequaltostring:(nsstring *)kuttypeimage]) { uiimage *img=info[uiimagepickercontrollereditedimage]; //use method [img fixorientation]; //here can perfect image [imguserimage setimage:img]; } }
another solution
//i found solution, may helpful you
//call method
image = [self scaleandrotateimage:image];
- (void)scaleandrotateimage:(uiimage *)image { int kmaxresolution = 320; // or whatever cgimageref imgref = image.cgimage; cgfloat width = cgimagegetwidth(imgref); cgfloat height = cgimagegetheight(imgref); cgaffinetransform transform = cgaffinetransformidentity; cgrect bounds = cgrectmake(0, 0, width, height); if (width > kmaxresolution || height > kmaxresolution) { cgfloat ratio = width/height; if (ratio > 1) { bounds.size.width = kmaxresolution; bounds.size.height = bounds.size.width / ratio; } else { bounds.size.height = kmaxresolution; bounds.size.width = bounds.size.height * ratio; } } cgfloat scaleratio = bounds.size.width / width; cgsize imagesize = cgsizemake(cgimagegetwidth(imgref), cgimagegetheight(imgref)); cgfloat boundheight; uiimageorientation orient = image.imageorientation; switch(orient) { case uiimageorientationup: //exif = 1 transform = cgaffinetransformidentity; break; case uiimageorientationupmirrored: //exif = 2 transform = cgaffinetransformmaketranslation(imagesize.width, 0.0); transform = cgaffinetransformscale(transform, -1.0, 1.0); break; case uiimageorientationdown: //exif = 3 transform = cgaffinetransformmaketranslation(imagesize.width, imagesize.height); transform = cgaffinetransformrotate(transform, m_pi); break; case uiimageorientationdownmirrored: //exif = 4 transform = cgaffinetransformmaketranslation(0.0, imagesize.height); transform = cgaffinetransformscale(transform, 1.0, -1.0); break; case uiimageorientationleftmirrored: //exif = 5 boundheight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundheight; transform = cgaffinetransformmaketranslation(imagesize.height, imagesize.width); transform = cgaffinetransformscale(transform, -1.0, 1.0); transform = cgaffinetransformrotate(transform, 3.0 * m_pi / 2.0); break; case uiimageorientationleft: //exif = 6 boundheight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundheight; transform = cgaffinetransformmaketranslation(0.0, imagesize.width); transform = cgaffinetransformrotate(transform, 3.0 * m_pi / 2.0); break; case uiimageorientationrightmirrored: //exif = 7 boundheight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundheight; transform = cgaffinetransformmakescale(-1.0, 1.0); transform = cgaffinetransformrotate(transform, m_pi / 2.0); break; case uiimageorientationright: //exif = 8 boundheight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundheight; transform = cgaffinetransformmaketranslation(imagesize.height, 0.0); transform = cgaffinetransformrotate(transform, m_pi / 2.0); break; default: [nsexception raise:nsinternalinconsistencyexception format:@"invalid image orientation"]; } uigraphicsbeginimagecontext(bounds.size); cgcontextref context = uigraphicsgetcurrentcontext(); if (orient == uiimageorientationright || orient == uiimageorientationleft) { cgcontextscalectm(context, -scaleratio, scaleratio); cgcontexttranslatectm(context, -height, 0); } else { cgcontextscalectm(context, scaleratio, -scaleratio); cgcontexttranslatectm(context, 0, -height); } cgcontextconcatctm(context, transform); cgcontextdrawimage(uigraphicsgetcurrentcontext(), cgrectmake(0, 0, width, height), imgref); uiimage *imagecopy = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); [self setrotatedimage:imagecopy]; //return imagecopy; }
Comments
Post a Comment