ios - Cropping UIImagePickerControllerOriginalImage using UIImagePickerControllerCropRect returns incorrect image -
i trying edit captured image , save gallery. have made
uiimagepickercontroller *picker=[[uiimagepickercontroller alloc] init]; picker.allowseditting=yes;
i want save image in editable square portion , save gallery. know can make use of [info objectforkey:@"uiimagepickercontrollereditedimage"]
save editted image. returns me image of dimension 320x320(ipad mini) , image of poor quality. planned crop original image [info objectforkey:@"uiimagepickercontrolleroriginalimage"]
using following code:
cgrect rect = [[info objectforkey:@"uiimagepickercontrollercroprect"]cgrectvalue]; uiimage *originalimage=[info objectforkey:@"uiimagepickercontrolleroriginalimage"]; cgimageref imageref = cgimagecreatewithimageinrect([originalimage cgimage], rect); uiimage *result = [uiimage imagewithcgimage:imageref scale:originalimage.scale orientation:originalimage.imageorientation]; cgimagerelease(imageref);
then saved both result image , edited image ([info objectforkey:@"uiimagepickercontrollereditedimage"]
). when compared both images, dint match. have attached edited , cropped images. ultimate aim crop original image image in editable square portion , save gallery image quality. can please tell me on goes wrong here , me fix issue?
thanks in advance.
i found out reason why cropping went wrong here. image returned uiimagepickercontrolleroriginalimage rotated -90 degrees. cropping on rotated image returned me incorrect cropped image. rotated image 90 degrees , cropped it. got expected cropped image quality. below code solved issue.
uiimage *originalimage = [info objectforkey:@"uiimagepickercontrolleroriginalimage"]; cgrect rect=[[info objectforkey:@"uiimagepickercontrollercroprect"]cgrectvalue]; uiimage *rotatedoriginalimage=[originalimage imagerotatedbydegrees:90.0]; cgimageref imageref = cgimagecreatewithimageinrect([rotatedoriginalimage cgimage], rect) ; uiimage *croppedimage = [uiimage imagewithcgimage:imageref];
code rotate image:
- (uiimage *)imagerotatedbydegrees:(cgfloat)degrees{ // calculate size of rotated view's containing box our drawing space uiview *rotatedviewbox = [[uiview alloc] initwithframe:cgrectmake(0,0,self.size.height, self.size.width)]; cgaffinetransform t = cgaffinetransformmakerotation(degreestoradians(degrees)); rotatedviewbox.transform = t; cgsize rotatedsize = rotatedviewbox.frame.size; // create bitmap context uigraphicsbeginimagecontext(rotatedsize); cgcontextref bitmap = uigraphicsgetcurrentcontext(); // move origin middle of image rotate , scale around center. cgcontexttranslatectm(bitmap, rotatedsize.width/2, rotatedsize.height/2); // // rotate image context cgcontextrotatectm(bitmap, degreestoradians(degrees)); // now, draw rotated/scaled image context cgcontextscalectm(bitmap, 1.0, -1.0); cgcontextdrawimage(bitmap, cgrectmake(-self.size.height / 2, -self.size.width / 2, self.size.height, self.size.width), [self cgimage]); uiimage *newimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return newimage; }
Comments
Post a Comment