规模和作物图像的ios(scale and crop image ios)

2019-06-26 11:25发布

我在为iOS的缩放和裁切图像(类似于卡马拉应用程序)功能的工作,下面的代码工作正常,只是产生的图像上来侧下来,我想知道为什么。

谢谢

- (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;

} 

Answer 1:

我也遇到了这个问题,没有任何解释。 但是发现,解决了这个问题的方法。 只要把你的代码如下两行,并再次尝试。 这个对我有用。

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


文章来源: scale and crop image ios
标签: ios5 scale crop