0

I have the exact problem as described here:

crop a rotated UIImage.

I need to crop a rotated UIImage, however, when I try the solution it doesn't crop the image

UIView *rotatedViewBox = [[UIView alloc] initWithFrame:videoPreviewView.frame];
NSLog(@"frame width: %f height:%f x:%f y:%f",videoPreviewView.frame.size.width, videoPreviewView.frame.size.height,videoPreviewView.frame.origin.x,videoPreviewView.frame.origin.y);
rotatedViewBox.transform = CGAffineTransformMakeRotation(90 * M_PI / 180);
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0f, rotatedSize.height / 2.0f);
CGContextRotateCTM(bitmap, 90 * M_PI / 180);
CGContextScaleCTM(bitmap, 1.0f, -1.0f);
CGContextDrawImage(bitmap, CGRectMake(-videoPreviewView.frame.size.width / 2.0f,
                                             -videoPreviewView.frame.size.height / 2.0f,
                                              videoPreviewView.frame.size.width,
                                              videoPreviewView.frame.size.height),
                           image.CGImage);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The log is: frame width:310 height:310 x:0 y: 69

The rotation works though but the new image is not cropped on 0,69,310,310 of the old image.

Community
  • 1
  • 1
Dejell
  • 13,947
  • 40
  • 146
  • 229

1 Answers1

0

You are using a "solution" which, although accepted, also carries the OP comment "but it's not working."

The solution only claims to deal with the rotation part of the problem, not the crop. It's doing what it claims to do, but no more.

Here's a CG function that performs a crop:

CGImageRef CGImageCreateWithImageInRect (
   CGImageRef image,
   CGRect rect
);

Input you image and the rect of the region you want to crop to.

See the answer here for an example use:
Cropping an UIImage

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
  • Where does the OP says that it's not working? How can I make it work based on this solution? as CGContextDrawImage supposed to draw and image inside another one isn't it? – Dejell Feb 03 '13 at 13:09
  • @Odelya, sorry I misread - that was _your_ comment about it not working! I've updated my answer – foundry Feb 03 '13 at 13:14
  • That wouldn't work. since it doesn't take in consideration the orientation (the user cropped the image BEFORE the orientation change) - I doubt if it will work but I will give it a chance now – Dejell Feb 03 '13 at 13:18
  • As I thought - the cropping doesn't work. This is why the previous OP posted his question - rotating a cropped image – Dejell Feb 03 '13 at 13:23
  • @Odelya, perhaps I don't understand what you want to do. If you turn the code in your question into a 'rotate' method, the code in my answer into a 'crop' method, apply the rotate method to you input image and then the crop method, you get a rotated, cropped result. That answers your question, "I need to crop a rotated UIImage". In your comment you talk of crop first, rotation second. Just reverse the order of the two methods. If this still doesn't answer your question can you update it to explain better what you are trying to do an what is not working. – foundry Feb 03 '13 at 14:05