簡體   English   中英

如何在UIImage中裁剪中心廣場?

[英]How to crop a center square in a UIImage?

對這個問題感到抱歉,但我在SO搜索了很多帖子,但我一無所獲。

問題是:如何在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;
}

添加更多信息,我在拍照后使用這個“裁剪”。

經過一些測試,我發現了一些有效的方法。

// 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM