ios - Cropping image to 1:1 and then resizing it -
i having problem in cropping image .
my flow :
1)crop image 1:1 (for example , if there image of size 665 x 985 px crop 665 x 665 px ).
2)resize 612 x 612 px same instagram .
for code use :
uiimage *image = [info objectforkey:uiimagepickercontrolleroriginalimage]; nsdata *d=uiimagepngrepresentation(image); cgfloat pic_crop; nslog(@"size of image(bytes):%lu",(unsigned long)[d length]); nslog(@" original image size :%.2f \n",(float)d.length/1024.0f/1024.0f); nslog(@"original height --->%f",image.size.height); nslog(@"original width --->%f",image.size.width); if (image.size.height > image.size.width) { pic_crop=image.size.width; nslog(@"%f",image.size.width); } else{ pic_crop=image.size.height; nslog(@"%f",image.size.height); }
then crop , if height less crop height size or width size !
_previewimage.frame = cgrectmake( _previewimage.frame.origin.x, _previewimage.frame.origin.y, pic_crop, pic_crop); uiimage *croppedimage = [image croppedimage:cgrectmake((image.size.width -_previewimage.frame.size.width)/2, (image.size.height -_previewimage.frame.size.height)/2, _previewimage.frame.size.width, _previewimage.frame.size.height)]; nslog(@"crop height --->%f",croppedimage.size.height); nslog(@"crop width --->%f",croppedimage.size.width); nsdata *d2=uiimagepngrepresentation(croppedimage); nslog(@"size of image(bytes):%lu",(unsigned long)[d2 length]); nslog(@" after compression :%.2f \n",(float)d2.length/1024.0f/1024.0f);
then resize 612 x 612 px ,
cgsize newsize; newsize = cgsizemake(612 , 612); uigraphicsbeginimagecontext( newsize ); [croppedimage drawinrect:cgrectmake(0,0,newsize.width,newsize.height)]; uiimage *newimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext();
this how perform thing , runs fine in simulator not in device !
it shrinks picture lot in device ! please me out ! in advance .
use uigraphicsbeginimagecontextwithoptions(newsize, yes, 0.0);
create image context adapted either retina or non-retina depending on device.
Comments
Post a Comment