Ios zoom and crop image

I am working on an image scaling and cropping feature (similar to camara app) for iOS, the code below works great, just the resulting image came down and I would like to understand why.

thank

- (UIImage*)imageByCropping:(UIImageView *)imageViewToCrop toRect:(CGRect)rect
{
    //create a context to do our clipping in
    CGRect newRect = CGRectApplyAffineTransform(rect, imageViewToCrop.transform);
    UIImage *imageToCrop = imageViewToCrop.image;
    UIGraphicsBeginImageContext(newRect.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    //create a rect with the size we want to crop the image to
    //the X and Y here are zero so we start at the beginning of our
    //newly created context
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    CGContextClipToRect( currentContext, clippedRect);


    //draw the image to our clipped context using our offset rect
    CGContextDrawImage(currentContext, newRect, imageToCrop.CGImage);

    //pull the image from our cropped context
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

    //pop the context to get back to the default
    UIGraphicsEndImageContext();

    return cropped;

} 

      

+3


source to share


1 answer


I also ran into this problem with no explanation. But found a workaround that solves this problem. Just put the next two lines in your code and try again. This works for me.



CGContextTranslateCTM(context, 0.0, newRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);//flip context

      

0


source







All Articles