ios - compress Size of Image without convert to NSData -
i have uicollectionview, in showing images downloading server. show these images using afnetworking class method setimagewithurl:(url*).
image size (968, 968) taking hell lot of memory. if convert these images nsdata , compressed uiimagejpegrepresentation method taking lot of time convert image nsdata. there way reduce size of images , show collection view cell without delay.
- (uiimage *)scaleandrotateimage:(uiimage *) image { //resolution want int kmaxresolution = 1024; 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(); return imagecopy;
}
Comments
Post a Comment