ios - Rotating UIImage causing some iPhones to crash -
on app, build pdf. add image pdf file. when use uiimagepickercontroller , select portrait photo, automatically rotates 90 degrees left. so, in order fix issue, add border around image, using code. however, on iphone 3gs, when adding larger photo, app crash. ideas on in code causing issue?
- (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); self.imagecopy = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); [self buildwithpic]; return self.imagecopy; }
Comments
Post a Comment