ios - Camera image rotation issue -
i facing strange problem here. when click image in portrait mode , upload , again fetch it displayed rotated 90 degrees counter clock wise. when see in camera roll displayed in correct orientation taken in. have tried possible links/codes issue nothing seemed help. save images in jpeg representation.please someone. in advance!!
resolved making category on uiimage , scaling , rotating image based on metadata exif's.
here's magical piece of code:
- (uiimage *)scaleandrotateimage:(uiimage *)image { int kmaxresolution = 640; // 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 = roundf(bounds.size.width / ratio); } else { bounds.size.height = kmaxresolution; bounds.size.width = roundf(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(); return imagecopy; }
Comments
Post a Comment