【问题标题】:How to crop a center square in a UIImage?如何裁剪 UIImage 中的中心正方形?
【发布时间】:2012-06-17 19:29:40
【问题描述】:

很抱歉这个问题,但我在 S.O. 中搜索了很多主题。我什么也没找到。

问题是:如何裁剪 UIImage 中的中心正方形?

我尝试了下面的代码,但没有成功。裁剪发生在左上角。

-(UIImage*)imageCrop:(UIImage*)original
{
    UIImage *ret = nil;

    // This calculates the crop area.

    float originalWidth  = original.size.width;
    float originalHeight = original.size.height;

    float edge = fminf(originalWidth, originalHeight);

    float posX = (originalWidth   - edge) / 2.0f;
    float posY = (originalHeight  - edge) / 2.0f;


    CGRect cropSquare = CGRectMake(posX, posY,
                                   edge, edge);


    // This performs the image cropping.

    CGImageRef imageRef = CGImageCreateWithImageInRect([original CGImage], cropSquare);

    ret = [UIImage imageWithCGImage:imageRef
                              scale:original.scale
                        orientation:original.imageOrientation];

    CGImageRelease(imageRef);

    return ret;
}

【问题讨论】:

    标签: ios uiimage crop


    【解决方案1】:

    添加更多信息,我在拍摄照片后使用此“裁剪”。

    经过一些测试,我发现了一些可行的方法。

    // If orientation indicates a change to portrait.
    if(original.imageOrientation == UIImageOrientationLeft ||
       original.imageOrientation == UIImageOrientationRight)
    {
        cropSquare = CGRectMake(posY, posX,
                                edge, edge);
    
    }
    else
    {
        cropSquare = CGRectMake(posX, posY,
                                edge, edge);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-21
      • 2017-11-10
      • 2017-06-13
      • 2010-12-26
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      相关资源
      最近更新 更多