Yoot
Yoot

Reputation: 1

how to "crop" a uiimage?

I'm making an app in which the user chooses an image, resize it by zooming (in a uiscrollview) and move it in the view. When finished, I would like to save the image like we can see it in the uiscrollview. Do you have any idea ?

Thanks.

Upvotes: 0

Views: 1233

Answers (1)

Ganesh
Ganesh

Reputation: 1079

Here is the code ......

Here frontGround is a imageview and testScrool is scroll view

hope this will work for you.

UIGraphicsBeginImageContext(testScrool.frame.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGRect clippedRect = CGRectMake(0, 0, testScrool.frame.size.width, testScrool.frame.size.height);
CGContextClipToRect(currentContext, clippedRect);
CGRect drawRect = CGRectMake(testScrool.frame.origin.x * -1,
                             testScrool.frame.origin.y * -1,frontGround.frame.size.width,                            frontGround.frame.size.height);
CGContextDrawImage(currentContext, drawRect, frontGround.image.CGImage);
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
frontGround.image = cropped;

Upvotes: 1

Related Questions