【问题标题】:scale and crop image ios缩放和裁剪图像 ios
【发布时间】:2012-07-21 23:49:15
【问题描述】:

我正在为 iOS 开发一个缩放和裁剪图像的功能(类似于 camara 应用程序),下面的代码可以正常工作,只是生成的图像是上下颠倒的,我想了解原因。

谢谢

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

} 

【问题讨论】:

    标签: ios objective-c uiimage scale crop


    【解决方案1】:

    我也遇到过这个问题,没有解释。但是找到了解决该问题的解决方法。只需将以下两行放入您的代码中,然后再试一次。它对我有用。

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

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 2011-11-16
      • 2016-08-29
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多